Skip to content

Commit

Permalink
fix: fixes coverage and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Jul 19, 2018
1 parent e46caae commit 09500c2
Show file tree
Hide file tree
Showing 36 changed files with 85 additions and 147 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "tsc -p tsconfig.build.json",
"build:watch": "tsc -p tsconfig.build.json -w",
"test:nolint": "npm run clean-build && node scripts/tests.js",
"test:nolint": "npm run clean-build && jest --clearCache && node scripts/tests.js",
"clean": "rimraf dist/**/* && rimraf tests/*/coverage && rimraf tests/*/debug.txt && rimraf tests/*/node_modules",
"clean-build": "npm run clean && npm run build",
"pretest": "npm run tslint && npm run clean-build",
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ const createTransformer = (options?: any): jest.Transformer => {
};
};

export default createTransformer();
export { createTransformer };
const mod = createTransformer();
mod.createTransformer = createTransformer;
export = mod;
7 changes: 6 additions & 1 deletion src/utils/get-cache-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export default function getCacheKey(
args: JestCacheKeyArguments,
ctx: TsJestContext,
): string {
const [fileData, filePath, jestConfigString, { instrument, rootDir }] = args;
const [
fileData,
filePath,
jestConfigString,
{ instrument = false, rootDir = process.cwd() } = {},
] = args;
const glob = JSON.parse(jestConfigString).globals || {};
return createHash('md5')
.update(MY_PACKAGE_CONTENT)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function flushLogs(): void {
return; // only output stuff for the first invocation and if logging is enabled.
}
logsFlushed = true;
const rootPath = path.resolve(__dirname, '../');
const rootPath = path.resolve(__dirname, '..', '..');
const JSONifiedLogs = logs.map(convertToJSONIfPossible);
const logString = JSONifiedLogs.join('\n');
const filePath = path.resolve(rootPath, 'debug.txt');
Expand Down
12 changes: 12 additions & 0 deletions tests/__tests__/__snapshots__/ts-coverage-async.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Typescript async coverage Should generate the correct async coverage numbers 1`] = `
"
=============================== Coverage summary ===============================
Statements : 68.75% ( 11/16 )
Branches : 33.33% ( 2/6 )
Functions : 66.67% ( 4/6 )
Lines : 73.33% ( 11/15 )
================================================================================
"
`;
12 changes: 12 additions & 0 deletions tests/__tests__/__snapshots__/ts-coverage.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Typescript coverage Should generate the correct coverage numbers. 1`] = `
"
=============================== Coverage summary ===============================
Statements : 68.75% ( 11/16 )
Branches : 33.33% ( 2/6 )
Functions : 66.67% ( 4/6 )
Lines : 73.33% ( 11/15 )
================================================================================
"
`;
10 changes: 5 additions & 5 deletions tests/__tests__/html-transform.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { process } from '../../src/preprocessor';
import * as tsJest from '../..';

const path = '/path/to/file.html';
const config = { globals: {} as any };
const config: jest.ProjectConfig = { globals: {} } as any;
// wrap a transformed source so that we can fake a `require()` on it by calling the returned wrapper
const wrap = (src: string) =>
new Function(`var module={}; ${src} return module.exports;`) as any;
Expand All @@ -14,10 +14,10 @@ const source = `<div class="html-test">
describe('Html transforms', () => {
it('transforms html if config.globals.__TRANSFORM_HTML__ is set', () => {
// get the untransformed version
const untransformed = process(source, path, config);
const untransformed = tsJest.process(source, path, config);
// ... then the one which should be transformed
config.globals.__TRANSFORM_HTML__ = true;
const transformed = process(source, path, config) as string;
(config.globals as any).__TRANSFORM_HTML__ = true;
const transformed = tsJest.process(source, path, config) as string;
// ... finally the result of a `require('module-with-transformed-version')`
const exported = wrap(transformed)();

Expand Down
23 changes: 14 additions & 9 deletions tests/__tests__/postprocess.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ jest.mock('@babel/core', () => {
import { getPostProcessHook } from '../../src/postprocess';

describe('postprocess', () => {
function runHook(tsCompilerOptions = {}, jestConfig = {}, tsJestConfig = {}) {
return getPostProcessHook(tsCompilerOptions, jestConfig, tsJestConfig)(
{ code: 'input_code', map: 'input_source_map' },
'fake_file',
{},
{ instrument: null },
);
function runHook(
tsCompilerOptions = {},
jestConfig: Partial<jest.ProjectConfig> = {},
tsJestConfig = {},
) {
return getPostProcessHook(
tsCompilerOptions,
jestConfig as any,
tsJestConfig,
)({ code: 'input_code', map: 'input_source_map' }, 'fake_file', {} as any, {
instrument: null,
});
}

it('skips postprocess when skipBabel=true', () => {
Expand All @@ -29,10 +34,10 @@ describe('postprocess', () => {
const transformMock = require.requireMock('@babel/core').transform;

runHook();
getPostProcessHook({}, {}, {})(
getPostProcessHook({}, {} as any, {})(
{ code: 'input_code', map: 'input_source_map' },
'fake_file',
{},
{} as any,
{ instrument: null },
);
expect(transformMock).lastCalledWith(
Expand Down
23 changes: 0 additions & 23 deletions tests/__tests__/transpile-if-ts.spec.ts

This file was deleted.

5 changes: 1 addition & 4 deletions tests/__tests__/ts-coverage-async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ describe('Typescript async coverage', () => {

const output = result.stdout;

expect(output).toContain('Statements : 71.43% ( 10/14 )');
expect(output).toContain('Branches : 33.33% ( 2/6 )');
expect(output).toContain('Functions : 66.67% ( 4/6 )');
expect(output).toContain('Lines : 66.67% ( 8/12 )');
expect(output).toMatchSnapshot();
});
});
5 changes: 1 addition & 4 deletions tests/__tests__/ts-coverage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ describe('Typescript coverage', () => {

const output = result.stdout;

expect(output).toContain('Statements : 71.43% ( 10/14 )');
expect(output).toContain('Branches : 33.33% ( 2/6 )');
expect(output).toContain('Functions : 66.67% ( 4/6 )');
expect(output).toContain('Lines : 66.67% ( 8/12 )');
expect(output).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion tests/button/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down
1 change: 0 additions & 1 deletion tests/coverage-simple/.gitignore

This file was deleted.

28 changes: 0 additions & 28 deletions tests/coverage-simple/Hello.ts

This file was deleted.

6 changes: 0 additions & 6 deletions tests/coverage-simple/NullCoverage.js

This file was deleted.

9 changes: 0 additions & 9 deletions tests/coverage-simple/__tests__/Hello.test.ts

This file was deleted.

22 changes: 0 additions & 22 deletions tests/coverage-simple/package.json

This file was deleted.

10 changes: 0 additions & 10 deletions tests/coverage-simple/tsconfig.json

This file was deleted.

4 changes: 0 additions & 4 deletions tests/coverage-simple/yarn.lock

This file was deleted.

2 changes: 1 addition & 1 deletion tests/dynamic-imports/jest.allowdefaultimports.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}
},
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down
2 changes: 1 addition & 1 deletion tests/dynamic-imports/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand Down
2 changes: 1 addition & 1 deletion tests/hoist-errors/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/hoist-test/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"moduleDirectories": ["node_modules", "src"],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
Expand Down
2 changes: 1 addition & 1 deletion tests/imports-test/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"moduleDirectories": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion tests/metadata-emit/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"moduleDirectories": ["node_modules", "src"],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
Expand Down
2 changes: 1 addition & 1 deletion tests/no-synthetic-default/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"moduleDirectories": ["node_modules", "src"],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
Expand Down
2 changes: 1 addition & 1 deletion tests/simple-async/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"jest": {
"rootDir": "./",
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/simple-long-path/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/skip-babelrc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"coverageReporters": [
Expand Down
2 changes: 1 addition & 1 deletion tests/synthetic-default/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"moduleDirectories": [
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion tests/ts-diagnostics/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/dist/index.js"
},
"moduleDirectories": [
"node_modules",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as tsJest from 'ts-jest';
import * as tsJest from '../../..';

describe('ts-jest module interface', () => {
it('is an object', () => {
Expand All @@ -10,4 +10,10 @@ describe('ts-jest module interface', () => {
it('has a getCacheKey function', () => {
expect(typeof tsJest.getCacheKey).toBe('function');
});
it('has a canInstrument property', () => {
expect(tsJest).toHaveProperty('canInstrument', true);
});
it('has a createTransformer function', () => {
expect(typeof tsJest.createTransformer).toBe('function');
});
});
Loading

0 comments on commit 09500c2

Please sign in to comment.