Skip to content

Commit

Permalink
fix: update for latest codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
teppeis committed Jun 24, 2018
1 parent dcfa445 commit 2b0eded
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 30 deletions.
37 changes: 29 additions & 8 deletions guess.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
'use strict';

const path = require('path');
const ts = require('typescript');

let pattern = 'test/**/*.@(ts|tsx)';
const cwd = process.cwd();
const packageData = require(path.join(cwd, 'package.json'));
const compilerOptions = loadCompilerOptions(cwd) || {};
const extensions = ['ts', 'tsx'];
if (compilerOptions.allowJs) {
extensions.push('js');
extensions.push('jsx');
}

let testDir = 'test';
const packageData = require(path.join(cwd, 'package.json'));
if (
packageData &&
typeof packageData.directories === 'object' &&
typeof packageData.directories.test === 'string'
) {
const testDir = packageData.directories.test;
pattern = `${testDir + (testDir.lastIndexOf('/', 0) === 0 ? '' : '/')}**/*.@(ts|tsx)`;
testDir = packageData.directories.test;
}
const pattern = path.join(testDir, `**/*.@(${extensions.join('|')})`);

require('./index')({
cwd: cwd,
pattern: pattern,
});
require('./index')({cwd, pattern, extensions});

function loadCompilerOptions(cwd) {
const tsconfigPath = ts.findConfigFile(cwd, ts.sys.fileExists);
if (!tsconfigPath) {
return null;
}
const result = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
if (result.error) {
throw new Error(result.error.messageText);
}
if (result.config && result.config.compilerOptions) {
const basepath = path.dirname(tsconfigPath);
const {options} = ts.parseJsonConfigFileContent(result.config, ts.sys, basepath);
return options;
}
return null;
}
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

'use strict';

const path = require('path');
const espowerSource = require('espower-source');
const minimatch = require('minimatch');
const tsNodeRegister = require('ts-node').register;

function espowerTypeScript(options) {
tsNodeRegister(options.tsNode);
espowerTsRegister('.ts', options);
espowerTsRegister('.tsx', options);
function espowerTypeScript(options, tsNodeOptions) {
tsNodeRegister(tsNodeOptions);
const {extensions = ['ts', 'tsx']} = options;
extensions.forEach(ext => {
espowerTsRegister(`.${ext}`, options);
});
}

function espowerTsRegister(ext, options) {
const cwd = options.cwd || process.cwd();
const separator = options.pattern.lastIndexOf('/', 0) === 0 ? '' : '/';
const pattern = cwd + separator + options.pattern;
const pattern = path.join(cwd, options.pattern);

const originalExtension = require.extensions[ext];
require.extensions[ext] = (module, filepath) => {
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"demo": "mocha --require './guess' test/demo.ts",
"lint": "eslint *.js",
"test": "run-s lint test:*",
"test:allow-js": "cd test/test-allow-js && mocha --require ../../guess test/*_test.js",
"test:outdir": "cd test/test-outdir && mocha --require ../../guess test/*_test.ts",
"test:ts": "mocha --require './guess' test/*_test.ts",
"test:tsx": "mocha --require './guess' test/*_test.tsx"
Expand Down
7 changes: 4 additions & 3 deletions test/lib/expectPowerAssertMessage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import assert = require('assert');
// don't use `assert` not to instrument
import ass = require('assert');

export default function expectPowerAssertMessage(body: () => void, expectedLines: string) {
try {
body();
assert.fail('AssertionError should be thrown');
ass.fail('AssertionError should be thrown');
} catch(e) {
assert.equal(e.message.split('\n').slice(2, -1).join('\n'), expectedLines);
ass.equal(e.message.split('\n').slice(2, -1).join('\n'), expectedLines);
}
};
29 changes: 17 additions & 12 deletions test/test-allow-js/test/to_be_instrumented_test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
'use strict';

import assert = require('assert')
import expect = require('expect.js')
import MyComponent from '../lib/mycomponent.tsx';
const assert = require('assert');

describe('test for allowJs option', function() {
describe('test for allowJs option', () => {
beforeEach(function() {
this.expectPowerAssertMessage = (body: () => void, expectedLines: string) => {
// don't use `assert` not to instrument
const ass = assert;
this.expectPowerAssertMessage = (body, expectedLines) => {
try {
body();
expect().fail('AssertionError should be thrown');
} catch(e) {
expect(e.message.split('\n').slice(2, -1).join('\n')).to.eql(expectedLines);
ass.fail('AssertionError should be thrown');
} catch (e) {
ass.equal(
e.message
.split('\n')
.slice(2, -1)
.join('\n'),
expectedLines
);
}
}
};
});

it('equal with Literal and Identifier: assert.equal(1, minusOne)', function() {
let minusOne: number = -1;
let expected: string =
` assert.equal(1, minusOne)
const minusOne = -1;
const expected = ` assert.equal(1, minusOne)
|
-1 `;
this.expectPowerAssertMessage(() => {
Expand Down

0 comments on commit 2b0eded

Please sign in to comment.