-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
401 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# Tests | ||
e2e | ||
test | ||
|
||
# Developement scripts | ||
scripts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
const base = require('../jest.config'); | ||
|
||
module.exports = { | ||
transform: { | ||
'\\.ts$': '<rootDir>/../dist/index.js', | ||
}, | ||
testRegex: '/__tests__/.+\\.test\\.ts$', | ||
collectCoverageFrom: ['<rootDir>/../src/**/*.ts'], | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'], | ||
testEnvironment: 'node', | ||
...base, | ||
rootDir: '.', | ||
testRegex: '/__tests__/.+\\.(test|spec)\\.ts$', | ||
coverageDirectory: '<rootDir>/../coverage/e2e', | ||
snapshotSerializers: ['<rootDir>/__serializers__/test-run-result.ts'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
module.exports = { | ||
rootDir: './test', | ||
rootDir: 'src', | ||
transform: { | ||
'\\.ts$': '<rootDir>/../dist/index.js', | ||
}, | ||
testRegex: '/.+\\.spec\\.ts$', | ||
collectCoverageFrom: ['<rootDir>/../src/**/*.ts'], | ||
testRegex: '\\.(spec|test)\\.ts$', | ||
coverageDirectory: '<rootDir>/../coverage/unit', | ||
collectCoverageFrom: [ | ||
'<rootDir>/../src/**/*.ts', | ||
'!<rootDir>/../src/**/*.spec.ts', | ||
'!<rootDir>/../src/**/*.test.ts', | ||
'!<rootDir>/../src/**/__*__/', | ||
], | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'], | ||
testEnvironment: 'node', | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export default function spyThese<T extends object, K extends keyof T>( | ||
object: T, | ||
implementations: { [key in K]: T[K] | any | undefined }, | ||
): { [key in K]: jest.SpyInstance<T[K]> } & { mockRestore: () => void } { | ||
const keys = Object.keys(implementations) as K[]; | ||
const res = keys.reduce( | ||
(map, key) => { | ||
const actual = object[key] as any; | ||
const spy = jest.spyOn(object, key as K); | ||
if (implementations[key]) { | ||
const impl = implementations[key] as (...args: any[]) => any; | ||
if (impl.length && /\W\$super\W/.test(impl.toString())) { | ||
spy.mockImplementation(function(this: T, ...args: any[]) { | ||
return impl.call(this, () => actual.apply(this, args), ...args); | ||
}); | ||
} else { | ||
spy.mockImplementation(impl); | ||
} | ||
} | ||
return map; | ||
}, | ||
{} as any, | ||
); | ||
// utility to restore all | ||
res.mockRestore = () => { | ||
keys.forEach(key => res[key].mockRestore()); | ||
}; | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as fakers from '../__helpers__/fakers'; | ||
import { TsJestConfig, TsJestProgram } from '../types'; | ||
import { ParsedCommandLine } from 'typescript'; | ||
|
||
// tslint:disable-next-line:variable-name | ||
export let __tsConfig: any = {}; | ||
|
||
export default class TsProgramMock implements TsJestProgram { | ||
get parsedConfig(): ParsedCommandLine { | ||
return __tsConfig; | ||
} | ||
|
||
constructor( | ||
public rootDir: string = fakers.filePath(''), | ||
public tsJestConfig: TsJestConfig = fakers.tsJestConfig(), | ||
) {} | ||
|
||
transpileModule(_: string, source: string) { | ||
return source; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...pshots__/ts-jest-transformer.spec.ts.snap → ...pshots__/ts-jest-transformer.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import TsJestTransformer from './ts-jest-transformer'; | ||
import * as fakers from './__helpers__/fakers'; | ||
import * as babelCfg from './utils/babel-config'; | ||
import * as closesPkgJson from './utils/closest-package-json'; | ||
import * as TsJestProgram from './ts-program'; | ||
|
||
jest.mock('./ts-program'); | ||
jest.mock('./utils/closest-package-json'); | ||
jest.mock('./utils/backports'); | ||
|
||
const mocks = { | ||
babelConfig: undefined as any, | ||
set packageJson(val: any) { | ||
(closesPkgJson as any).__default = val; | ||
}, | ||
set tsConfig(val: any) { | ||
(TsJestProgram as any).__tsConfig = val; | ||
}, | ||
reset() { | ||
this.babelConfig = undefined; | ||
this.packageJson = { name: 'mock' }; | ||
this.tsConfig = {}; | ||
}, | ||
}; | ||
beforeAll(() => { | ||
jest | ||
.spyOn(babelCfg, 'loadDefault') | ||
.mockImplementation(() => mocks.babelConfig); | ||
}); | ||
afterEach(() => { | ||
mocks.reset(); | ||
}); | ||
|
||
describe('process', () => { | ||
describe('hoisting', () => { | ||
const transformer = new TsJestTransformer(); | ||
it('should hoist jest.mock calls using babel', () => { | ||
const config = fakers.jestConfig({}, { babelJest: true }); | ||
const result = transformer.process( | ||
fakers.transpiledTsSource(), | ||
fakers.filePath('path/to/file.ts'), | ||
config, | ||
) as jest.TransformedSource; | ||
expect(result.code).toMatchSnapshot(); | ||
}); | ||
}); // hoisting | ||
}); // process | ||
|
||
describe('getCacheKey', () => { | ||
const fakeSource = fakers.typescriptSource(); | ||
const fakeFilePath = fakers.filePath('file.ts'); | ||
const fakeJestConfig = JSON.stringify( | ||
fakers.jestConfig({}, { babelJest: true }), | ||
); | ||
|
||
const call: typeof TsJestTransformer['prototype']['getCacheKey'] = ( | ||
// tslint:disable-next-line:trailing-comma | ||
...args | ||
) => new TsJestTransformer().getCacheKey(...args); | ||
const defaultCall = () => call(fakeSource, fakeFilePath, fakeJestConfig); | ||
|
||
it('should be a 28 chars string, different for each case', () => { | ||
const allCacheKeys = [ | ||
defaultCall(), | ||
call('const b = 2', fakeFilePath, fakeJestConfig), | ||
call(fakeSource, fakers.filePath('other-file.ts'), fakeJestConfig), | ||
call(fakeSource, fakeFilePath, '{"rootDir": "./sub"}'), | ||
call(fakeSource, fakeFilePath, fakeJestConfig, { instrument: true }), | ||
call(fakeSource, fakeFilePath, fakeJestConfig, { rootDir: '/child' }), | ||
]; | ||
|
||
mocks.babelConfig = '{sourceMaps: true}'; | ||
allCacheKeys.push(defaultCall()); | ||
mocks.reset(); | ||
|
||
mocks.tsConfig = '{"files": []}'; | ||
allCacheKeys.push(defaultCall()); | ||
mocks.reset(); | ||
|
||
mocks.packageJson = '{"name": "dummy"}'; | ||
allCacheKeys.push(defaultCall()); | ||
mocks.reset(); | ||
|
||
// uniq array should equal original | ||
expect( | ||
allCacheKeys.filter((k, i) => allCacheKeys.indexOf(k) === i), | ||
).toEqual(allCacheKeys); | ||
allCacheKeys.forEach(cacheKey => { | ||
expect(cacheKey).toHaveLength(28); | ||
}); | ||
}); // all different and 28 chars | ||
}); // getCacheKey |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.