Skip to content

Commit 25e1c63

Browse files
authored
Merge pull request #751 from huafu/fix-cache-collisions
Fix cache collisions
2 parents a6bcec4 + 02b1439 commit 25e1c63

33 files changed

Lines changed: 279 additions & 218 deletions

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ end_of_line = lf
77
insert_final_newline = true
88
trim_trailing_whitespace = true
99

10-
[*.md,*.snap]
10+
[*.md]
11+
trim_trailing_whitespace = false
12+
13+
[*.snap]
1114
trim_trailing_whitespace = false

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ jspm_packages
4343

4444
.vscode
4545
.idea
46-
46+
.cache
4747

4848
# tests specific
4949
tests/simple-long-path/long-src-path
5050
# is linked to the temp dir of the os
5151
e2e/__workdir_synlink__
5252
**/.ts-jest-e2e.json
53+
.ts-jest-digest
5354
# while refactoring...
5455
old/
5556

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ tsconfig.build.json
6969
tslint.json
7070
.npmrc
7171
.markdownlint.yaml
72+
.cache
7273

7374
# Github Pages
7475
docs
7576

7677
commitlint.config.js
78+
79+
# ensure we do not omit the digest
80+
!.ts-jest-digest

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ env:
66
cache:
77
npm: true
88
directories:
9+
- .cache
910
- /tmp/ts-jest-e2e-workdir/__templates__
1011

1112
node_js:
@@ -36,9 +37,9 @@ script:
3637
# only grab coverage data on node 10
3738
- |
3839
if [[ "$(node --version)" == v10.* ]]; then
39-
npm run test -- --coverage;
40+
npm run test -- --runInBand --coverage;
4041
else
41-
npm run test;
42+
npm run test -- --runInBand;
4243
fi
4344
4445
after_success:

appveyor.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ install:
2121
- ps: Install-Product node 8 x64
2222
- npm install -g npm@^5
2323
# Typical npm stuff.
24-
- set CI=true
24+
# - set CI=true
2525
# Our E2E work dir
2626
- set TS_JEST_E2E_WORKDIR=%APPDATA%\ts-jest-e2e
2727
- npm ci --ignore-scripts
2828
- npm run clean -- --when-ci-commit-message
2929

3030
cache:
31+
- .cache -> package.json
3132
- '%APPDATA%\npm-cache'
3233
- '%APPDATA%\ts-jest-e2e\__templates__'
3334

3435
# Post-install test scripts.
3536
test_script:
36-
- cmd: npm run test
37+
- cmd: npm run test -- --runInBand
3738

3839
# skip_commits:
3940
# files:

e2e/__cases__/deep/src/Tests/jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ module.exports = Object.assign({}, cfg, {
2020
tsConfig: "./tsconfig.json",
2121
},
2222
},
23-
// testResultsProcessor: "jest-teamcity-reporter",
2423
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createTransformer } from 'ts-jest'
2+
import { readFileSync } from 'fs'
3+
4+
describe('tsConfig', () => {
5+
it('should have digest and versions', () => {
6+
const tr = createTransformer()
7+
const cs = tr.configsFor({} as any)
8+
expect(cs.tsJestDigest).toHaveLength(40)
9+
expect(cs.tsJestDigest).toBe(readFileSync(require.resolve('ts-jest/.ts-jest-digest'), 'utf8'))
10+
expect(cs.versions['ts-jest']).toBe(require('ts-jest/package.json').version)
11+
})
12+
})

e2e/__helpers__/test-case/run-result.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { readFileSync, realpathSync } from 'fs'
44
import { tmpdir } from 'os'
55
import { resolve, sep } from 'path'
66

7+
import { cacheDir } from '../../../scripts/lib/paths'
8+
79
import ProcessedFileIo from './processed-file-io'
810
import { escapeRegex, normalizeJestOutput, stripAnsiColors } from './utils'
911

@@ -17,6 +19,7 @@ export default class RunResult {
1719
cmd: string
1820
args: string[]
1921
env: { [key: string]: string }
22+
config: jest.InitialOptions
2023
}>,
2124
) {}
2225
get logFilePath() {
@@ -56,9 +59,11 @@ export default class RunResult {
5659
return normalizeJestOutput(this.stdout)
5760
}
5861
get cmdLine() {
59-
return [this.context.cmd, ...this.context.args]
60-
.filter(a => !['-u', '--updateSnapshot', '--runInBand', '--'].includes(a))
61-
.join(' ')
62+
return this.normalize(
63+
[this.context.cmd, ...this.context.args]
64+
.filter(a => !['-u', '--updateSnapshot', '--runInBand', '--'].includes(a))
65+
.join(' '),
66+
)
6267
}
6368

6469
ioFor(relFilePath: string): ProcessedFileIo {
@@ -80,7 +85,12 @@ export default class RunResult {
8085
const realCwd = realpathSync(cwd)
8186
const tmp = tmpdir()
8287
const realTmp = realpathSync(tmp)
83-
const map = [{ from: cwd, to: '<cwd>' }, { from: tmp, to: '<tmp>' }, { from: /\b[a-f0-9]{40}\b/g, to: '<hex:40>' }]
88+
const map = [
89+
{ from: cwd, to: '<cwd>' },
90+
{ from: tmp, to: '<tmp>' },
91+
{ from: /\b[a-f0-9]{40}\b/g, to: '<hex:40>' },
92+
{ from: cacheDir, to: '<ts-jest-cache>' },
93+
]
8494
if (cwd !== realCwd) map.push({ from: realCwd, to: '<cwd>' })
8595
if (tmp !== realTmp) map.push({ from: realTmp, to: '<tmp>' })
8696
if (sep === '\\') {

e2e/__helpers__/test-case/runtime.ts

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
writeFileSync,
1717
} from 'fs-extra'
1818
import merge from 'lodash.merge'
19-
import { join, relative, sep } from 'path'
19+
import { join, relative, resolve, sep } from 'path'
2020

2121
import * as Paths from '../../../scripts/lib/paths'
2222

@@ -49,19 +49,35 @@ function hooksSourceWith(vars: Record<string, any>): string {
4949
}
5050

5151
export function run(name: string, options: RunTestOptions = {}): RunResult {
52-
const { args = [], env = {}, template, inject, writeIo } = options
52+
const {
53+
args = [],
54+
env = {},
55+
template,
56+
inject,
57+
writeIo,
58+
noCache,
59+
jestConfigPath: configFile = 'jest.config.js',
60+
} = options
5361
const { workdir: dir, sourceDir, hooksFile, ioDir } = prepareTest(
5462
name,
5563
template || templateNameForPath(join(Paths.e2eSourceDir, name)),
5664
options,
5765
)
58-
const pkg = require(join(dir, 'package.json'))
66+
const pkg = readJsonSync(join(dir, 'package.json'))
67+
68+
// grab base configuration
69+
const jestConfigPath = resolve(dir, configFile)
70+
let baseConfig: jest.InitialOptions = require(jestConfigPath)
71+
if (configFile === 'package.json') baseConfig = (baseConfig as any).jest
72+
73+
const extraConfig = {} as jest.InitialOptions
5974

6075
let shortCmd: string
6176
let cmdArgs: string[] = []
6277
if (inject) {
63-
cmdArgs.push('--testPathPattern="/__eval\\\\.ts$"')
64-
} // '--testRegex=""'
78+
extraConfig.testMatch = undefined
79+
extraConfig.testRegex = '/__eval\\.ts$'
80+
}
6581
if (process.argv.find(v => ['--updateSnapshot', '-u'].includes(v))) {
6682
cmdArgs.push('-u')
6783
}
@@ -77,25 +93,42 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
7793
shortCmd = 'jest'
7894
}
7995

80-
// merge given config extend
81-
if (options.jestConfig || options.tsJestConfig) {
82-
let originalConfig: any = join(dir, 'jest.config.js')
83-
if (existsSync(originalConfig)) {
84-
originalConfig = require(originalConfig)
96+
// check/merge config
97+
if (cmdArgs.includes('--config')) {
98+
throw new Error(`Extend config using tsJestConfig and jestConfig options, not thru args.`)
99+
}
100+
if (cmdArgs.includes('--no-cache')) {
101+
throw new Error(`Use the noCache option to disable cache, not thru args.`)
102+
}
103+
// extends config
104+
if (options.jestConfig) {
105+
merge(extraConfig, options.jestConfig)
106+
}
107+
if (options.tsJestConfig) {
108+
const globalConfig: any = extraConfig.globals || (extraConfig.globals = {})
109+
const tsJestConfig = globalConfig['ts-jest'] || (globalConfig['ts-jest'] = {})
110+
merge(tsJestConfig, options.tsJestConfig)
111+
}
112+
113+
if (noCache || writeIo) {
114+
cmdArgs.push('--no-cache')
115+
} else if (!(baseConfig.cacheDirectory || extraConfig.cacheDirectory)) {
116+
// force the cache directory if not set
117+
extraConfig.cacheDirectory = join(Paths.cacheDir, `e2e-${template}`)
118+
}
119+
120+
// write final config
121+
const finalConfig = merge({}, baseConfig, extraConfig)
122+
if (Object.keys(extraConfig).length !== 0) {
123+
if (configFile === 'package.json') {
124+
pkg.jest = finalConfig
125+
outputJsonSync(jestConfigPath, pkg)
85126
} else {
86-
originalConfig = require(join(dir, 'package.json')).jest || {}
127+
outputFileSync(jestConfigPath, `module.exports = ${JSON.stringify(finalConfig, null, 2)}`, 'utf8')
87128
}
88-
cmdArgs.push(
89-
'--config',
90-
JSON.stringify(
91-
merge({}, originalConfig, options.jestConfig, {
92-
globals: { 'ts-jest': options.tsJestConfig || {} },
93-
}),
94-
),
95-
)
96129
}
97130

98-
// run in band
131+
// ensure we run in band
99132
if (!cmdArgs.includes('--runInBand')) {
100133
cmdArgs.push('--runInBand')
101134
}
@@ -142,6 +175,7 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
142175
args: cmdArgs,
143176
env: mergedEnv,
144177
ioDir: writeIo ? ioDir : undefined,
178+
config: finalConfig,
145179
})
146180
}
147181

e2e/__helpers__/test-case/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export interface RunTestOptions {
1010
writeIo?: boolean
1111
jestConfig?: jest.ProjectConfig | any
1212
tsJestConfig?: TsJestConfig | any
13+
noCache?: boolean
14+
jestConfigPath?: string
1315
}
1416

1517
export type RunWithTemplatesIterator = (runtTest: () => RunResult, context: RunWithTemplateIteratorContext) => void

0 commit comments

Comments
 (0)