Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instrument .js when allowJs is enabled #51

Merged
merged 2 commits into from
Jun 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/**/*.js
/node_modules
test/test-outdir/**/*.js
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);
}
};
9 changes: 9 additions & 0 deletions test/test-allow-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "test-allow-js",
"version": "1.0.0",
"description": "This is a dummy file. See ../../package.json",
"devDependencies": {
},
"dependencies": {
}
}
34 changes: 34 additions & 0 deletions test/test-allow-js/test/to_be_instrumented_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const assert = require('assert');

describe('test for allowJs option', () => {
beforeEach(function() {
// don't use `assert` not to instrument
const ass = assert;
this.expectPowerAssertMessage = (body, expectedLines) => {
try {
body();
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() {
const minusOne = -1;
const expected = ` assert.equal(1, minusOne)
|
-1 `;
this.expectPowerAssertMessage(() => {
assert.equal(1, minusOne);
}, expected);
});
});
13 changes: 13 additions & 0 deletions test/test-allow-js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"outDir": "build",
"module": "commonjs",
"target": "ES5",
"noImplicitAny": true,
"jsx": "react"
},
"exclude": [
"node_modules"
]
}