Skip to content

Commit bd55448

Browse files
committed
feat: cache key + tests
1 parent f2e1da2 commit bd55448

24 files changed

Lines changed: 401 additions & 106 deletions

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Tests
22
e2e
3-
test
43

54
# Developement scripts
65
scripts

e2e/jest.config.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
const base = require('../jest.config');
2+
13
module.exports = {
2-
transform: {
3-
'\\.ts$': '<rootDir>/../dist/index.js',
4-
},
5-
testRegex: '/__tests__/.+\\.test\\.ts$',
6-
collectCoverageFrom: ['<rootDir>/../src/**/*.ts'],
7-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
8-
testEnvironment: 'node',
4+
...base,
5+
rootDir: '.',
6+
testRegex: '/__tests__/.+\\.(test|spec)\\.ts$',
7+
coverageDirectory: '<rootDir>/../coverage/e2e',
98
snapshotSerializers: ['<rootDir>/__serializers__/test-run-result.ts'],
109
};

jest.config.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
module.exports = {
2-
rootDir: './test',
2+
rootDir: 'src',
33
transform: {
44
'\\.ts$': '<rootDir>/../dist/index.js',
55
},
6-
testRegex: '/.+\\.spec\\.ts$',
7-
collectCoverageFrom: ['<rootDir>/../src/**/*.ts'],
6+
testRegex: '\\.(spec|test)\\.ts$',
7+
coverageDirectory: '<rootDir>/../coverage/unit',
8+
collectCoverageFrom: [
9+
'<rootDir>/../src/**/*.ts',
10+
'!<rootDir>/../src/**/*.spec.ts',
11+
'!<rootDir>/../src/**/*.test.ts',
12+
'!<rootDir>/../src/**/__*__/',
13+
],
814
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
915
testEnvironment: 'node',
1016
};

package-lock.json

Lines changed: 5 additions & 0 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
@@ -47,6 +47,7 @@
4747
"devDependencies": {
4848
"@types/babel__core": "^7.0.1",
4949
"@types/fs-extra": "5.0.4",
50+
"@types/gist-package-json": "git+https://gist.github.com/5c1cc527fe6b5b7dba41fec7fe54bf6e.git",
5051
"@types/jest": "^23.3.1",
5152
"@types/node": "^10.5.7",
5253
"closest-file-data": "^0.1.4",
Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { resolve } from 'path';
1+
import { TsJestGlobalOptions } from '../types';
2+
import { resolve, relative } from 'path';
3+
import spyThese from './spy-these';
4+
import realFs from 'fs';
25

3-
export function filePathMock(relPath: string): string {
6+
export function filePath(relPath: string): string {
47
return resolve(__dirname, '..', '..', relPath);
58
}
69

7-
export function transpiledTsSourceMock() {
10+
export const rootDir = filePath('');
11+
12+
export function transpiledTsSource() {
813
return `
914
"use strict";
1015
var __importDefault = (this && this.__importDefault) || function (mod) {
@@ -23,7 +28,7 @@ describe('hello', function () {
2328
`;
2429
}
2530

26-
export function tsSourceMock() {
31+
export function typescriptSource() {
2732
return `
2833
import upper from './upper';
2934
import lower from './lower';
@@ -39,3 +44,24 @@ describe('hello', () => {
3944
});
4045
`;
4146
}
47+
48+
export function tsJestConfig<T extends TsJestGlobalOptions>(
49+
options?: TsJestGlobalOptions,
50+
): T {
51+
return { ...options } as any;
52+
}
53+
54+
export function jestConfig<T extends jest.ProjectConfig>(
55+
options?: jest.InitialOptions,
56+
tsJestOptions?: TsJestGlobalOptions,
57+
): T {
58+
const res = {
59+
globals: {},
60+
moduleFileExtensions: ['ts', 'js'],
61+
...options,
62+
} as any;
63+
if (tsJestOptions) {
64+
res.globals['ts-jest'] = tsJestConfig(tsJestOptions);
65+
}
66+
return res;
67+
}

src/__helpers__/spy-these.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default function spyThese<T extends object, K extends keyof T>(
2+
object: T,
3+
implementations: { [key in K]: T[K] | any | undefined },
4+
): { [key in K]: jest.SpyInstance<T[K]> } & { mockRestore: () => void } {
5+
const keys = Object.keys(implementations) as K[];
6+
const res = keys.reduce(
7+
(map, key) => {
8+
const actual = object[key] as any;
9+
const spy = jest.spyOn(object, key as K);
10+
if (implementations[key]) {
11+
const impl = implementations[key] as (...args: any[]) => any;
12+
if (impl.length && /\W\$super\W/.test(impl.toString())) {
13+
spy.mockImplementation(function(this: T, ...args: any[]) {
14+
return impl.call(this, () => actual.apply(this, args), ...args);
15+
});
16+
} else {
17+
spy.mockImplementation(impl);
18+
}
19+
}
20+
return map;
21+
},
22+
{} as any,
23+
);
24+
// utility to restore all
25+
res.mockRestore = () => {
26+
keys.forEach(key => res[key].mockRestore());
27+
};
28+
return res;
29+
}

src/__mocks__/ts-program.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as fakers from '../__helpers__/fakers';
2+
import { TsJestConfig, TsJestProgram } from '../types';
3+
import { ParsedCommandLine } from 'typescript';
4+
5+
// tslint:disable-next-line:variable-name
6+
export let __tsConfig: any = {};
7+
8+
export default class TsProgramMock implements TsJestProgram {
9+
get parsedConfig(): ParsedCommandLine {
10+
return __tsConfig;
11+
}
12+
13+
constructor(
14+
public rootDir: string = fakers.filePath(''),
15+
public tsJestConfig: TsJestConfig = fakers.tsJestConfig(),
16+
) {}
17+
18+
transpileModule(_: string, source: string) {
19+
return source;
20+
}
21+
}

test/__snapshots__/ts-jest-transformer.spec.ts.snap renamed to src/__snapshots__/ts-jest-transformer.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`hoisting should hoist jest.mock calls using babel 1`] = `
3+
exports[`process hoisting should hoist jest.mock calls using babel 1`] = `
44
"
55
\\"use strict\\";
66
File renamed without changes.

0 commit comments

Comments
 (0)