Skip to content

Commit

Permalink
Merge pull request #751 from huafu/fix-cache-collisions
Browse files Browse the repository at this point in the history
Fix cache collisions
  • Loading branch information
huafu committed Sep 22, 2018
2 parents a6bcec4 + 02b1439 commit 25e1c63
Show file tree
Hide file tree
Showing 33 changed files with 279 additions and 218 deletions.
5 changes: 4 additions & 1 deletion .editorconfig
Expand Up @@ -7,5 +7,8 @@ end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md,*.snap]
[*.md]
trim_trailing_whitespace = false

[*.snap]
trim_trailing_whitespace = false
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -43,13 +43,14 @@ jspm_packages

.vscode
.idea

.cache

# tests specific
tests/simple-long-path/long-src-path
# is linked to the temp dir of the os
e2e/__workdir_synlink__
**/.ts-jest-e2e.json
.ts-jest-digest
# while refactoring...
old/

Expand Down
4 changes: 4 additions & 0 deletions .npmignore
Expand Up @@ -69,8 +69,12 @@ tsconfig.build.json
tslint.json
.npmrc
.markdownlint.yaml
.cache

# Github Pages
docs

commitlint.config.js

# ensure we do not omit the digest
!.ts-jest-digest
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -6,6 +6,7 @@ env:
cache:
npm: true
directories:
- .cache
- /tmp/ts-jest-e2e-workdir/__templates__

node_js:
Expand Down Expand Up @@ -36,9 +37,9 @@ script:
# only grab coverage data on node 10
- |
if [[ "$(node --version)" == v10.* ]]; then
npm run test -- --coverage;
npm run test -- --runInBand --coverage;
else
npm run test;
npm run test -- --runInBand;
fi
after_success:
Expand Down
5 changes: 3 additions & 2 deletions appveyor.yml
Expand Up @@ -21,19 +21,20 @@ install:
- ps: Install-Product node 8 x64
- npm install -g npm@^5
# Typical npm stuff.
- set CI=true
# - set CI=true
# Our E2E work dir
- set TS_JEST_E2E_WORKDIR=%APPDATA%\ts-jest-e2e
- npm ci --ignore-scripts
- npm run clean -- --when-ci-commit-message

cache:
- .cache -> package.json
- '%APPDATA%\npm-cache'
- '%APPDATA%\ts-jest-e2e\__templates__'

# Post-install test scripts.
test_script:
- cmd: npm run test
- cmd: npm run test -- --runInBand

# skip_commits:
# files:
Expand Down
1 change: 0 additions & 1 deletion e2e/__cases__/deep/src/Tests/jest.config.js
Expand Up @@ -20,5 +20,4 @@ module.exports = Object.assign({}, cfg, {
tsConfig: "./tsconfig.json",
},
},
// testResultsProcessor: "jest-teamcity-reporter",
})
12 changes: 12 additions & 0 deletions e2e/__cases__/ts-jest-checks/index.spec.ts
@@ -0,0 +1,12 @@
import { createTransformer } from 'ts-jest'
import { readFileSync } from 'fs'

describe('tsConfig', () => {
it('should have digest and versions', () => {
const tr = createTransformer()
const cs = tr.configsFor({} as any)
expect(cs.tsJestDigest).toHaveLength(40)
expect(cs.tsJestDigest).toBe(readFileSync(require.resolve('ts-jest/.ts-jest-digest'), 'utf8'))
expect(cs.versions['ts-jest']).toBe(require('ts-jest/package.json').version)
})
})
18 changes: 14 additions & 4 deletions e2e/__helpers__/test-case/run-result.ts
Expand Up @@ -4,6 +4,8 @@ import { readFileSync, realpathSync } from 'fs'
import { tmpdir } from 'os'
import { resolve, sep } from 'path'

import { cacheDir } from '../../../scripts/lib/paths'

import ProcessedFileIo from './processed-file-io'
import { escapeRegex, normalizeJestOutput, stripAnsiColors } from './utils'

Expand All @@ -17,6 +19,7 @@ export default class RunResult {
cmd: string
args: string[]
env: { [key: string]: string }
config: jest.InitialOptions
}>,
) {}
get logFilePath() {
Expand Down Expand Up @@ -56,9 +59,11 @@ export default class RunResult {
return normalizeJestOutput(this.stdout)
}
get cmdLine() {
return [this.context.cmd, ...this.context.args]
.filter(a => !['-u', '--updateSnapshot', '--runInBand', '--'].includes(a))
.join(' ')
return this.normalize(
[this.context.cmd, ...this.context.args]
.filter(a => !['-u', '--updateSnapshot', '--runInBand', '--'].includes(a))
.join(' '),
)
}

ioFor(relFilePath: string): ProcessedFileIo {
Expand All @@ -80,7 +85,12 @@ export default class RunResult {
const realCwd = realpathSync(cwd)
const tmp = tmpdir()
const realTmp = realpathSync(tmp)
const map = [{ from: cwd, to: '<cwd>' }, { from: tmp, to: '<tmp>' }, { from: /\b[a-f0-9]{40}\b/g, to: '<hex:40>' }]
const map = [
{ from: cwd, to: '<cwd>' },
{ from: tmp, to: '<tmp>' },
{ from: /\b[a-f0-9]{40}\b/g, to: '<hex:40>' },
{ from: cacheDir, to: '<ts-jest-cache>' },
]
if (cwd !== realCwd) map.push({ from: realCwd, to: '<cwd>' })
if (tmp !== realTmp) map.push({ from: realTmp, to: '<tmp>' })
if (sep === '\\') {
Expand Down
74 changes: 54 additions & 20 deletions e2e/__helpers__/test-case/runtime.ts
Expand Up @@ -16,7 +16,7 @@ import {
writeFileSync,
} from 'fs-extra'
import merge from 'lodash.merge'
import { join, relative, sep } from 'path'
import { join, relative, resolve, sep } from 'path'

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

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

export function run(name: string, options: RunTestOptions = {}): RunResult {
const { args = [], env = {}, template, inject, writeIo } = options
const {
args = [],
env = {},
template,
inject,
writeIo,
noCache,
jestConfigPath: configFile = 'jest.config.js',
} = options
const { workdir: dir, sourceDir, hooksFile, ioDir } = prepareTest(
name,
template || templateNameForPath(join(Paths.e2eSourceDir, name)),
options,
)
const pkg = require(join(dir, 'package.json'))
const pkg = readJsonSync(join(dir, 'package.json'))

// grab base configuration
const jestConfigPath = resolve(dir, configFile)
let baseConfig: jest.InitialOptions = require(jestConfigPath)
if (configFile === 'package.json') baseConfig = (baseConfig as any).jest

const extraConfig = {} as jest.InitialOptions

let shortCmd: string
let cmdArgs: string[] = []
if (inject) {
cmdArgs.push('--testPathPattern="/__eval\\\\.ts$"')
} // '--testRegex=""'
extraConfig.testMatch = undefined
extraConfig.testRegex = '/__eval\\.ts$'
}
if (process.argv.find(v => ['--updateSnapshot', '-u'].includes(v))) {
cmdArgs.push('-u')
}
Expand All @@ -77,25 +93,42 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
shortCmd = 'jest'
}

// merge given config extend
if (options.jestConfig || options.tsJestConfig) {
let originalConfig: any = join(dir, 'jest.config.js')
if (existsSync(originalConfig)) {
originalConfig = require(originalConfig)
// check/merge config
if (cmdArgs.includes('--config')) {
throw new Error(`Extend config using tsJestConfig and jestConfig options, not thru args.`)
}
if (cmdArgs.includes('--no-cache')) {
throw new Error(`Use the noCache option to disable cache, not thru args.`)
}
// extends config
if (options.jestConfig) {
merge(extraConfig, options.jestConfig)
}
if (options.tsJestConfig) {
const globalConfig: any = extraConfig.globals || (extraConfig.globals = {})
const tsJestConfig = globalConfig['ts-jest'] || (globalConfig['ts-jest'] = {})
merge(tsJestConfig, options.tsJestConfig)
}

if (noCache || writeIo) {
cmdArgs.push('--no-cache')
} else if (!(baseConfig.cacheDirectory || extraConfig.cacheDirectory)) {
// force the cache directory if not set
extraConfig.cacheDirectory = join(Paths.cacheDir, `e2e-${template}`)
}

// write final config
const finalConfig = merge({}, baseConfig, extraConfig)
if (Object.keys(extraConfig).length !== 0) {
if (configFile === 'package.json') {
pkg.jest = finalConfig
outputJsonSync(jestConfigPath, pkg)
} else {
originalConfig = require(join(dir, 'package.json')).jest || {}
outputFileSync(jestConfigPath, `module.exports = ${JSON.stringify(finalConfig, null, 2)}`, 'utf8')
}
cmdArgs.push(
'--config',
JSON.stringify(
merge({}, originalConfig, options.jestConfig, {
globals: { 'ts-jest': options.tsJestConfig || {} },
}),
),
)
}

// run in band
// ensure we run in band
if (!cmdArgs.includes('--runInBand')) {
cmdArgs.push('--runInBand')
}
Expand Down Expand Up @@ -142,6 +175,7 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
args: cmdArgs,
env: mergedEnv,
ioDir: writeIo ? ioDir : undefined,
config: finalConfig,
})
}

Expand Down
2 changes: 2 additions & 0 deletions e2e/__helpers__/test-case/types.ts
Expand Up @@ -10,6 +10,8 @@ export interface RunTestOptions {
writeIo?: boolean
jestConfig?: jest.ProjectConfig | any
tsJestConfig?: TsJestConfig | any
noCache?: boolean
jestConfigPath?: string
}

export type RunWithTemplatesIterator = (runtTest: () => RunResult, context: RunWithTemplateIteratorContext) => void
Expand Down
10 changes: 5 additions & 5 deletions e2e/__tests__/__snapshots__/diagnostics.test.ts.snap
Expand Up @@ -116,7 +116,7 @@ exports[`With diagnostics, first throws should fail using template "with-typescr
`;

exports[`With diagnostics, warn only should pass using template "default" 1`] = `
√ jest --config {"preset":"ts-jest","testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"diagnostics":{"warnOnly":true}}}}
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -138,7 +138,7 @@ exports[`With diagnostics, warn only should pass using template "default" 1`] =
`;

exports[`With diagnostics, warn only should pass using template "with-babel-6" 1`] = `
√ jest --config {"preset":"ts-jest","testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"babelConfig":true,"diagnostics":{"warnOnly":true}}}}
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -160,7 +160,7 @@ exports[`With diagnostics, warn only should pass using template "with-babel-6" 1
`;

exports[`With diagnostics, warn only should pass using template "with-babel-7" 1`] = `
√ jest --config {"preset":"ts-jest","testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"babelConfig":true,"diagnostics":{"warnOnly":true}}}}
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -182,7 +182,7 @@ exports[`With diagnostics, warn only should pass using template "with-babel-7" 1
`;

exports[`With diagnostics, warn only should pass using template "with-jest-22" 1`] = `
√ jest --config {"transform":{"^.+\\\\.tsx?$":"ts-jest"},"testMatch":["**/__tests__/**/*.js?(x)","**/?(*.)+(spec|test).js?(x)","**/__tests__/**/*.ts?(x)","**/?(*.)+(spec|test).ts?(x)"],"moduleFileExtensions":["js","json","jsx","node","ts","tsx"],"testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"diagnostics":{"warnOnly":true}}}}
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -204,7 +204,7 @@ exports[`With diagnostics, warn only should pass using template "with-jest-22" 1
`;

exports[`With diagnostics, warn only should pass using template "with-typescript-2-7" 1`] = `
√ jest --config {"preset":"ts-jest","testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"diagnostics":{"ignoreCodes":[5023],"warnOnly":true}}}}
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand Down
10 changes: 5 additions & 5 deletions e2e/__tests__/__snapshots__/hoisting.test.ts.snap
Expand Up @@ -58,7 +58,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "defaul
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "default": output 1`] = `
√ jest
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -134,7 +134,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-b
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-6": output 1`] = `
√ jest
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -218,7 +218,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-b
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-babel-7": output 1`] = `
√ jest
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -293,7 +293,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-j
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-jest-22": output 1`] = `
√ jest
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -368,7 +368,7 @@ exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-t
`;
exports[`Hoisting jest.mock() & jest.unmock() should pass using template "with-typescript-2-7": output 1`] = `
√ jest
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand Down

0 comments on commit 25e1c63

Please sign in to comment.