Skip to content
This repository was archived by the owner on Jan 6, 2020. It is now read-only.

Improvements #7

Merged
merged 4 commits into from
Jan 17, 2017
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
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- '7'
- '6'
- '5'
- '4'
- '0.12'
- '0.10'
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"babel-types": "^6.7.2"
},
"devDependencies": {
"ava": "^0.14.0",
"ava": "^0.17.0",
"babel-core": "^6.7.5",
"xo": "^0.15.1"
"xo": "^0.17.0"
}
}
69 changes: 32 additions & 37 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,38 @@ function addExample(input, output) {
);
}

const HELPER = [
'function _avaThrowsHelper(fn, data) {',
' try {',
' return fn();',
' } catch (e) {',
' var type = typeof e;',
'',
' if (e !== null && (type === "object" || type === "function")) {',
' try {',
' Object.defineProperty(e, "_avaThrowsHelperData", {',
' value: data',
' });',
' } catch (e) {}',
' }',
'',
' throw e;',
' }',
'}',
''
].join('\n');
const HELPER = `function _avaThrowsHelper(fn, data) {
try {
return fn();
} catch (e) {
var type = typeof e;

if (e !== null && (type === "object" || type === "function")) {
try {
Object.defineProperty(e, "_avaThrowsHelperData", {
value: data
});
} catch (e) {}
}

throw e;
}
}\n`;

function wrapped(throws, expression, line, column) {
return [
`t.${throws}(_avaThrowsHelper(function () {`,
` return ${expression};`,
'}, {',
` line: ${line},`,
` column: ${column},`,
` source: "${expression}",`,
` filename: "some-file.js"`,
'}));'
].join('\n');
return `t.${throws}(_avaThrowsHelper(function () {
return ${expression};
}, {
line: ${line},
column: ${column},
source: "${expression}",
filename: "some-file.js"
}));`;
}

test('creates a helper', t => {
const input = `t.throws(foo())`;
const code = transform(input).code;
const input = 't.throws(foo())';
const {code} = transform(input);

const expected = [
HELPER,
Expand All @@ -81,8 +76,8 @@ test('creates a helper', t => {
});

test('creates the helper only once', t => {
const input = `t.throws(foo());\nt.throws(bar());`;
const code = transform(input).code;
const input = 't.throws(foo());\nt.throws(bar());';
const {code} = transform(input);

const expected = [
HELPER,
Expand All @@ -96,15 +91,15 @@ test('creates the helper only once', t => {

test('does nothing if it does not match', t => {
const input = 't.is(foo());';
const code = transform(input).code;
const {code} = transform(input);

t.is(code, input);
addExample(input, code);
});

test('helps notThrows', t => {
const input = `t.notThrows(baz())`;
const code = transform(input).code;
const input = 't.notThrows(baz())';
const {code} = transform(input);

const expected = [
HELPER,
Expand Down