Skip to content

Commit

Permalink
Issue kulshekhar#8 fix. Test and dependencies refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Igmat committed Oct 7, 2016
1 parent e5320ff commit 4ea466c
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 20 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
},
"devDependencies": {
"react": "^15.3.2",
"react-test-renderer": "^15.3.2"
"react-test-renderer": "^15.3.2",
"jest": "16.0.1",
"cross-spawn": "4.0.2"
}
}
15 changes: 3 additions & 12 deletions preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const tsc = require('typescript');
const {getTSConfig} = require('./utils');

module.exports = {
process(src, path, config) {
Expand All @@ -10,19 +11,9 @@ module.exports = {
fileName: path
});

const modified = `require('ts-jest').install({environment: 'node', emptyCacheBetweenOperations: true});${transpiled.outputText}`;

return modified;
return transpiled.outputText;
}

return src;
}
};

function getTSConfig(globals) {
const config = globals.__TS_CONFIG__ || {};
config.module = config.module || tsc.ModuleKind.CommonJS;
config.jsx = config.jsx || tsc.JsxEmit.React;

return tsc.convertCompilerOptionsFromJson(config).options;
}
};
4 changes: 2 additions & 2 deletions tests/__helpers__/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const {fileExists} = require('./utils');
const path = require('path');
const spawnSync = require('child_process').spawnSync;
const spawnSync = require('cross-spawn').sync;

// assuming that jest is installed globally
// using `npm i -g jest-cli`
const JEST_PATH = 'jest';
const JEST_PATH = 'jest';

// return the result of the spawned proccess:
// [ 'status', 'signal', 'output', 'pid', 'stdout', 'stderr',
Expand Down
2 changes: 1 addition & 1 deletion tests/__helpers__/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// from: https://github.com/facebook/jest/blob/master/integration_tests/utils.js

const {spawnSync} = require('child_process');
const {spawnSync} = require('cross-spawn').sync;
const fs = require('fs');
const path = require('path');

Expand Down
23 changes: 23 additions & 0 deletions tests/__mocks__/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const path = require.requireActual('path');
const mockePath = jest.genMockFromModule('path');

// This is a custom function that our tests can use during setup to specify
// what the files on the "mock" filesystem should look like when any of the
// `fs` APIs are used.
let baseDir = '';
function __setBaseDir(newBaseDir) {
baseDir = newBaseDir;
}

// A custom version of `readdirSync` that reads from the special mocked out
// file list set via __setMockFiles
function resolve(args) {
return path.resolve(baseDir, args)
}

mockePath.__setBaseDir = __setBaseDir;
mockePath.resolve = resolve;

module.exports = mockePath;
21 changes: 21 additions & 0 deletions tests/__tests__/ts-compilation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const runJest = require('../__helpers__/runJest');

describe('hello_world', () => {

it('should run successfully', () => {

const result = runJest('../simple', ['--no-cache']);

const stderr = result.stderr.toString();
const output = result.output.toString();

expect(result.status).toBe(1);
expect(output).toContain('1 failed, 1 total');
expect(stderr).toContain('Hello Class');
expect(stderr).toContain('should throw an error on line 11');

});

});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('hello_world', () => {
const result = runJest('../simple', ['--no-cache']);

const stderr = result.stderr.toString();
const output = result.output.toString();

expect(result.status).toBe(1);
expect(stderr).toContain('Hello.ts:11:11');
Expand Down
36 changes: 36 additions & 0 deletions tests/__tests__/tsconfig-default.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

jest.mock('path');

describe('hello_world', () => {

beforeEach(() => {
// Set up some mocked out file info before each test
require('path').__setBaseDir('./tests/tsconfig-default');
});

it('should run successfully', () => {
const {getTSConfig} = require('../../utils');
const result = getTSConfig();

expect(result).toEqual ({
"sourceMap": false,
"declaration": false,
"target": 2,
"module": 1,
"moduleResolution": 2,
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false,
"noImplicitReturns": true,
"removeComments": true,
"strictNullChecks": true,
"jsx": 2,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": false
});

});

});
38 changes: 38 additions & 0 deletions tests/__tests__/tsconfig-string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

jest.mock('path');

describe('hello_world', () => {

beforeEach(() => {
// Set up some mocked out file info before each test
require('path').__setBaseDir('./tests/tsconfig-string');
});

it('should run successfully', () => {
const {getTSConfig} = require('../../utils');
const result = getTSConfig({
"__TS_CONFIG__": "my-tsconfig.json"
});

expect(result).toEqual ({
"sourceMap": false,
"declaration": false,
"target": 2,
"module": 1,
"moduleResolution": 2,
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false,
"noImplicitReturns": true,
"removeComments": true,
"strictNullChecks": true,
"jsx": 2,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": false
});

});

});
21 changes: 21 additions & 0 deletions tests/__tests__/tsx-compilation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const runJest = require('../__helpers__/runJest');

describe('button', () => {

it('should run successfully', () => {

const result = runJest('../button', ['--no-cache', '-u']);

const stderr = result.stderr.toString();
const output = result.output.toString();

expect(result.status).toBe(1);
expect(output).toContain('1 failed, 1 passed, 2 total');
expect(stderr).toContain('Button renders correctly');
expect(stderr).toContain('BadButton should throw an error on line 18');

});

});
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ const runJest = require('../__helpers__/runJest');

describe('button', () => {

it('should run successfully', () => {
it('should show the correct error locations in the typescript files', () => {

const result = runJest('../button', ['--no-cache', '-u']);

const stderr = result.stderr.toString();
const output = result.output.toString();

expect(result.status).toBe(1);
expect(output).toContain('1 failed, 1 passed, 2 total');
expect(stderr).toContain('✓ Button renders correctly');
expect(stderr).toContain('✕ BadButton should throw an error on line 18');
expect(stderr).toContain('Button.tsx:18:23');
expect(stderr).toContain('Button.test.tsx:15:12');

Expand Down
19 changes: 19 additions & 0 deletions tests/tsconfig-default/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"sourceMap": false,
"declaration": false,
"target": "ES6",
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false,
"noImplicitReturns": true,
"removeComments": true,
"strictNullChecks": true,
"jsx": "react",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": false
}
}
19 changes: 19 additions & 0 deletions tests/tsconfig-string/my-tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"sourceMap": false,
"declaration": false,
"target": "ES6",
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": false,
"noImplicitReturns": true,
"removeComments": true,
"strictNullChecks": true,
"jsx": "react",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": false
}
}
20 changes: 20 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const tsc = require('typescript');
const path = require('path');

function getTSConfig(globals) {
let config = globals && globals.__TS_CONFIG__;
if (config === undefined) {
config = require(path.resolve('tsconfig.json')).compilerOptions;
}
if (typeof config === 'string') {
config = require(path.resolve(config)).compilerOptions;
}
config.module = config.module || tsc.ModuleKind.CommonJS;
config.jsx = config.jsx || tsc.JsxEmit.React;

return tsc.convertCompilerOptionsFromJson(config).options;
}

module.exports = {
getTSConfig
}

0 comments on commit 4ea466c

Please sign in to comment.