Skip to content

Commit

Permalink
ci(cache): improves cache handling & use a non-deleted dir
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Sep 22, 2018
1 parent 2715f73 commit 02b1439
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -43,7 +43,7 @@ jspm_packages

.vscode
.idea

.cache

# tests specific
tests/simple-long-path/long-src-path
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Expand Up @@ -69,6 +69,7 @@ tsconfig.build.json
tslint.json
.npmrc
.markdownlint.yaml
.cache

# Github Pages
docs
Expand Down
1 change: 1 addition & 0 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
1 change: 1 addition & 0 deletions appveyor.yml
Expand Up @@ -28,6 +28,7 @@ install:
- npm run clean -- --when-ci-commit-message

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

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",
},
},
cacheDirectory: '../../node_modules/.cache',
})
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
87 changes: 47 additions & 40 deletions e2e/__helpers__/test-case/runtime.ts
Expand Up @@ -49,26 +49,40 @@ function hooksSourceWith(vars: Record<string, any>): string {
}

export function run(name: string, options: RunTestOptions = {}): RunResult {
const { args = [], env = {}, template, inject, writeIo, noCache } = 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')
}
cmdArgs.push(...args)
let isScriptWithConfig = false
if (!inject && pkg.scripts && pkg.scripts.test) {
isScriptWithConfig = /['" ]\-\-config['" ]/.test(pkg.scripts.test)
if (cmdArgs.length) {
cmdArgs.unshift('--')
}
Expand All @@ -80,49 +94,41 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
}

// check/merge config
if (isScriptWithConfig) {
throw new Error(`Cannot extend config of a test case using a package script which has '--config' in command line.`)
}
let originalConfig: any = join(dir, 'jest.config.js')
if (cmdArgs.includes('--config')) {
const confArg = cmdArgs[cmdArgs.indexOf('--config') + 1]
if (confArg.startsWith('{')) {
originalConfig = JSON.parse(confArg)
} else {
originalConfig = require(resolve(dir, confArg))
}
} else if (existsSync(originalConfig)) {
originalConfig = require(originalConfig)
} else {
originalConfig = require(join(dir, 'package.json')).jest || {}
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.`)
}
let finalConfig = originalConfig as jest.InitialOptions
// extends config
if (options.jestConfig || options.tsJestConfig) {
finalConfig = merge({}, originalConfig, options.jestConfig, {
globals: { 'ts-jest': options.tsJestConfig || {} },
})
cmdArgs.push(
'--config',
JSON.stringify(
merge({}, originalConfig, options.jestConfig, {
globals: { 'ts-jest': options.tsJestConfig || {} },
}),
),
)
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)
}

// verify that we have the cache directory set
let hasCaching = !(cmdArgs.includes('--no-cache') || finalConfig.cache === false)
if (hasCaching && (noCache || writeIo)) {
if (noCache || writeIo) {
cmdArgs.push('--no-cache')
hasCaching = false
} else if (!(baseConfig.cacheDirectory || extraConfig.cacheDirectory)) {
// force the cache directory if not set
extraConfig.cacheDirectory = join(Paths.cacheDir, `e2e-${template}`)
}
if (hasCaching && !finalConfig.cacheDirectory) {
throw new Error(`The cacheDirectory is not set in the config`)

// 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 {
outputFileSync(jestConfigPath, `module.exports = ${JSON.stringify(finalConfig, null, 2)}`, 'utf8')
}
}

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

Expand Down
1 change: 1 addition & 0 deletions e2e/__helpers__/test-case/types.ts
Expand Up @@ -11,6 +11,7 @@ export interface RunTestOptions {
jestConfig?: jest.ProjectConfig | any
tsJestConfig?: TsJestConfig | any
noCache?: boolean
jestConfigPath?: string
}

export type RunWithTemplatesIterator = (runtTest: () => RunResult, context: RunWithTemplateIteratorContext) => void
Expand Down
1 change: 0 additions & 1 deletion e2e/__templates__/default/jest.config.js
Expand Up @@ -2,5 +2,4 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {} } },
cacheDirectory: './node_modules/.cache',
}
1 change: 0 additions & 1 deletion e2e/__templates__/with-babel-6/jest.config.js
Expand Up @@ -2,5 +2,4 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {}, babelConfig: true } },
cacheDirectory: './node_modules/.cache',
}
1 change: 0 additions & 1 deletion e2e/__templates__/with-babel-7/jest.config.js
Expand Up @@ -2,5 +2,4 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {}, babelConfig: true } },
cacheDirectory: './node_modules/.cache',
}
1 change: 0 additions & 1 deletion e2e/__templates__/with-jest-22/jest.config.js
Expand Up @@ -3,5 +3,4 @@ const { jestPreset } = require('ts-jest')
module.exports = Object.assign({}, jestPreset, {
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {} } },
cacheDirectory: './node_modules/.cache',
})
1 change: 0 additions & 1 deletion e2e/__templates__/with-typescript-2-7/jest.config.js
Expand Up @@ -2,5 +2,4 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {}, diagnostics: { ignoreCodes: [5023] } } },
cacheDirectory: './node_modules/.cache',
}
1 change: 0 additions & 1 deletion e2e/__templates__/with-unsupported-version/jest.config.js
Expand Up @@ -2,5 +2,4 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {}, diagnostics: { ignoreCodes: [5023, 5024] } } },
cacheDirectory: './node_modules/.cache',
}
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}}},"cacheDirectory":"./node_modules/.cache"} --no-cache
√ 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}}},"cacheDirectory":"./node_modules/.cache"} --no-cache
√ 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}}},"cacheDirectory":"./node_modules/.cache"} --no-cache
√ 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}}},"cacheDirectory":"./node_modules/.cache"} --no-cache
√ 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}}},"cacheDirectory":"./node_modules/.cache"} --no-cache
√ jest --no-cache
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand Down
10 changes: 5 additions & 5 deletions e2e/__tests__/__snapshots__/isolated-modules.test.ts.snap
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`With isolatedModules enabled should pass using template "default" 1`] = `
√ jest --config {"preset":"ts-jest","testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"isolatedModules":true}},"cacheDirectory":"./node_modules/.cache"}
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -18,7 +18,7 @@ exports[`With isolatedModules enabled should pass using template "default" 1`] =
`;

exports[`With isolatedModules enabled should pass using template "with-babel-6" 1`] = `
√ jest --config {"preset":"ts-jest","testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"babelConfig":true,"isolatedModules":true}},"cacheDirectory":"./node_modules/.cache"}
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -35,7 +35,7 @@ exports[`With isolatedModules enabled should pass using template "with-babel-6"
`;

exports[`With isolatedModules enabled should pass using template "with-babel-7" 1`] = `
√ jest --config {"preset":"ts-jest","testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"babelConfig":true,"isolatedModules":true}},"cacheDirectory":"./node_modules/.cache"}
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -52,7 +52,7 @@ exports[`With isolatedModules enabled should pass using template "with-babel-7"
`;

exports[`With isolatedModules enabled 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":{},"isolatedModules":true}},"cacheDirectory":"./node_modules/.cache"}
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand All @@ -69,7 +69,7 @@ exports[`With isolatedModules enabled should pass using template "with-jest-22"
`;

exports[`With isolatedModules enabled should pass using template "with-typescript-2-7" 1`] = `
√ jest --config {"preset":"ts-jest","testEnvironment":"node","globals":{"ts-jest":{"tsConfig":{},"diagnostics":{"ignoreCodes":[5023]},"isolatedModules":true}},"cacheDirectory":"./node_modules/.cache"}
√ jest
↳ exit code: 0
===[ STDOUT ]===================================================================
Expand Down

0 comments on commit 02b1439

Please sign in to comment.