Skip to content

Commit ce7afaf

Browse files
committed
fix: normalizes bundle hash on any node version
1 parent d03e0e7 commit ce7afaf

10 files changed

Lines changed: 113 additions & 20 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ jspm_packages
4949
tests/simple-long-path/long-src-path
5050
# is linked to the temp dir of the os
5151
e2e/__workdir_synlink__
52+
**/.ts-jest-e2e.json
5253
# while refactoring...
5354
old/
5455

.npmignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,18 @@ jspm_packages
5151
old
5252

5353
*.tgz
54+
55+
# others
56+
.npmignore
57+
.eslintrc.js
58+
jest.config.js
59+
.vscode
60+
.github
61+
appveyor.yml
62+
.editorconfig
63+
.prettierignore
64+
tsconfig.json
65+
.prettierrc
66+
.travis.yml
67+
tsconfig.build.json
68+
tslint.json

e2e/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ module.exports = {
1010
'<rootDir>/e2e/__serializers__/test-run-result.ts',
1111
'<rootDir>/e2e/__serializers__/test-file-io.ts',
1212
],
13+
verbose: true,
1314
}

package-lock.json

Lines changed: 53 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"doctoc": "^1.3.1",
7272
"eslint": "^5.4.0",
7373
"fs-extra": "^7.0.0",
74+
"glob-gitignore": "^1.0.9",
7475
"husky": "^0.14.3",
7576
"jest": "^23.4.1",
7677
"js-yaml": "^3.12.0",

scripts/e2e.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ const path = require('path')
88
const Paths = require('./lib/paths')
99
const { createHash } = require('crypto')
1010
const logger = require('./lib/logger')
11-
const { createBundle, realDigest } = require('./lib/bundle')
11+
const { createBundle, packageDigest } = require('./lib/bundle')
1212
const npm = require('./lib/npm')
1313

14-
const configFile = path.resolve(__dirname, '..', 'e2e', 'jest.config.js')
14+
const configFile = path.join(Paths.e2eRootDir, 'jest.config.js')
15+
1516
let parentArgs = process.argv.slice(2)
1617
if (parentArgs.includes('--coverage')) {
1718
logger.warn(
1819
'Coverages cannot be activated for e2e tests (but can in each e2e test).'
1920
)
2021
parentArgs = parentArgs.filter(a => a !== '--coverage')
2122
}
22-
if (!parentArgs.includes('--verbose')) parentArgs.push('--verbose')
2323

2424
function getDirectories(rootDir) {
2525
return fs.readdirSync(rootDir).filter(function(file) {
@@ -30,7 +30,7 @@ function getDirectories(rootDir) {
3030
function sha1(...data) {
3131
const hash = createHash('sha1')
3232
data.forEach(item => hash.update(item))
33-
return hash.digest('base64').toString()
33+
return hash.digest('hex').toString()
3434
}
3535

3636
function log(...msg) {
@@ -48,8 +48,8 @@ function setupE2e() {
4848

4949
// get the hash of the bundle (to know if we should install it again or not)
5050
// we need to compute it ourselfs as the npm pack creates different tgz even tho content has not changed
51-
const bundleHash = realDigest(bundle)
52-
log('bundled ts-jest digest:', bundleHash)
51+
const bundleHash = packageDigest(bundle)
52+
log('ts-jest digest:', bundleHash)
5353

5454
// ensure directory exists before copying over
5555
fs.mkdirpSync(Paths.e2eWorkTemplatesDir)

scripts/lib/bundle.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
const npm = require('./npm')
22
const logger = require('./logger')
33
const { rootDir } = require('./paths')
4-
const { join } = require('path')
4+
const { join, resolve } = require('path')
5+
const { readFileSync, statSync } = require('fs')
6+
const { createHash } = require('crypto')
7+
const { sync: globIgnore } = require('glob-gitignore')
58

69
// This will trigger the build as well (not using yarn since yarn pack is bugy)
710
// Except that on npm < 4.0.0 the prepare doesn't exists
811

9-
module.exports = function createBundle() {
12+
function createBundle(log = logger.log.bind(logger)) {
1013
if (!npm.can.prepare()) {
11-
logger.log('building ts-jest')
14+
log('building ts-jest')
1215
npm.spawnSync(['-s', 'run', 'build'], { cwd: rootDir })
1316
}
14-
logger.log('creating ts-jest bundle')
17+
log('creating ts-jest bundle')
1518
const res = npm.spawnSync(['-s', 'pack'], { cwd: rootDir })
1619
return join(rootDir, res.stdout.toString().trim())
1720
}
21+
22+
function packageDigest() {
23+
const files = globIgnore(join(rootDir, '**'), {
24+
ignore: readFileSync(join(rootDir, '.npmignore'))
25+
.toString('utf8')
26+
.split(/\n/g)
27+
.filter(l => l && !/^(\s*#.+|\s*)$/.test(l)),
28+
})
29+
const hash = createHash('sha1')
30+
files.sort().forEach(file => {
31+
if (statSync(file).isDirectory()) return
32+
hash.update(readFileSync(resolve(file)))
33+
hash.update('\x00')
34+
})
35+
return hash.digest('hex')
36+
}
37+
38+
module.exports = {
39+
createBundle,
40+
packageDigest,
41+
}

scripts/test-external-project.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { tmpdir } = require('os')
77
const { rootDir } = require('./lib/paths')
88
const { existsSync, realpathSync } = require('fs')
99
const logger = require('./lib/logger')
10-
const createBundle = require('./lib/bundle')
10+
const { createBundle } = require('./lib/bundle')
1111
const npm = require('./lib/npm')
1212

1313
let projectPath = process.argv[2]

src/lib/debug.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('debug', () => {
1919
__setup()
2020
debug('foo')
2121
expect(stdoutSpy).toHaveBeenCalledTimes(1)
22-
expect(stdoutSpy).toHaveBeenCalledWith('ts-jest: foo\n')
22+
expect(stdoutSpy.mock.calls[0][0]).toBe('ts-jest: foo\n')
2323
})
2424
it('should NOT log to stdout when TS_JEST_DEBUG is falsy', () => {
2525
process.env.TS_JEST_DEBUG = ''
@@ -40,14 +40,14 @@ describe('warn', () => {
4040
__setup()
4141
warn('foo')
4242
expect(stderrSpy).toHaveBeenCalledTimes(1)
43-
expect(stderrSpy).toHaveBeenCalledWith('ts-jest: foo\n')
43+
expect(stderrSpy.mock.calls[0][0]).toBe('ts-jest: foo\n')
4444
})
4545
it('should log to stderr even when TS_JEST_DEBUG is falsy', () => {
4646
delete process.env.TS_JEST_DEBUG
4747
__setup()
4848
warn('foo')
4949
expect(stderrSpy).toHaveBeenCalledTimes(1)
50-
expect(stderrSpy).toHaveBeenCalledWith('ts-jest: foo\n')
50+
expect(stderrSpy.mock.calls[0][0]).toBe('ts-jest: foo\n')
5151
})
5252
})
5353

@@ -60,7 +60,7 @@ describe('wrapWithDebug', () => {
6060
__setup()
6161
expect(wrapAndCall('bar')).toBe('hello bar')
6262
expect(stdoutSpy).toHaveBeenCalledTimes(1)
63-
expect(stdoutSpy).toHaveBeenCalledWith('ts-jest: foo\n')
63+
expect(stdoutSpy.mock.calls[0][0]).toBe('ts-jest: foo\n')
6464
})
6565
it('should NOT log to stdout when TS_JEST_DEBUG is falsy', () => {
6666
process.env.TS_JEST_DEBUG = ''

src/lib/debug.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ export const defaultLogger: Logger = (
2121
...args: any[]
2222
) => {
2323
// we use stderr/stdout dirrectly so that the log won't be swallowed by jest
24+
// the dummy calback is to ensure output on CI with node 6
2425
if (kind === 'warn') {
25-
process.stderr.write(format(msg, ...args) + '\n')
26+
process.stderr.write(format(msg, ...args) + '\n', () => undefined)
2627
} else if (kind) {
27-
process.stdout.write(format(msg, ...args) + '\n')
28+
process.stdout.write(format(msg, ...args) + '\n', () => undefined)
2829
}
2930
}
3031

0 commit comments

Comments
 (0)