Skip to content

Commit 3660901

Browse files
committed
refactor: extract expectPowerAssertMessage()
1 parent 6a72cfc commit 3660901

File tree

4 files changed

+21
-47
lines changed

4 files changed

+21
-47
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import expect = require('expect.js');
2+
3+
export default function expectPowerAssertMessage(body: () => void, expectedLines: string) {
4+
try {
5+
body();
6+
expect().fail('AssertionError should be thrown');
7+
} catch(e) {
8+
expect(e.message.split('\n').slice(2, -1).join('\n')).to.eql(expectedLines);
9+
}
10+
};

test/test-outdir/test/to_be_instrumented_test.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
'use strict';
22

3-
import assert = require('assert')
4-
import expect = require('expect.js')
5-
import MyComponent from '../lib/mycomponent.tsx';
3+
import assert = require('assert');
4+
import expectPowerAssertMessage from '../../lib/expectPowerAssertMessage';
65

76
describe('espower-typescript: `outDir` option', function() {
8-
beforeEach(function() {
9-
this.expectPowerAssertMessage = (body: () => void, expectedLines: string) => {
10-
try {
11-
body();
12-
expect().fail('AssertionError should be thrown');
13-
} catch(e) {
14-
expect(e.message.split('\n').slice(2, -1).join('\n')).to.eql(expectedLines);
15-
}
16-
}
17-
});
18-
197
it('equal with Literal and Identifier: assert.equal(1, minusOne)', function() {
208
let minusOne: number = -1;
219
let expected: string =
2210
` assert.equal(1, minusOne)
2311
|
2412
-1 `;
25-
this.expectPowerAssertMessage(() => {
13+
expectPowerAssertMessage(() => {
2614
assert.equal(1, minusOne);
2715
}, expected);
2816
});

test/to_be_instrumented_test.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
'use strict';
22

3-
import assert = require('assert')
4-
import expect = require('expect.js')
3+
import assert = require('assert');
4+
import expectPowerAssertMessage from './lib/expectPowerAssertMessage';
55
import MyComponent from './lib/mycomponent.tsx';
66

77
describe('espower-typescript: ts', function() {
8-
9-
beforeEach(function() {
10-
this.expectPowerAssertMessage = (body: () => void, expectedLines: string) => {
11-
try {
12-
body();
13-
expect().fail('AssertionError should be thrown');
14-
} catch(e) {
15-
expect(e.message.split('\n').slice(2, -1).join('\n')).to.eql(expectedLines);
16-
}
17-
}
18-
});
19-
208
it('Nested CallExpression with BinaryExpression: assert((three * (seven * ten)) === three)', function() {
219
let one: number = 1;
2210
let two: number = 2;
@@ -35,7 +23,7 @@ describe('espower-typescript: ts', function() {
3523
=> 3
3624
[number] three * (seven * ten)
3725
=> 210`;
38-
this.expectPowerAssertMessage(() => {
26+
expectPowerAssertMessage(() => {
3927
assert(three * (seven * ten) === three);
4028
}, expected);
4129
});
@@ -46,7 +34,7 @@ describe('espower-typescript: ts', function() {
4634
` assert.equal(1, minusOne)
4735
|
4836
-1 `;
49-
this.expectPowerAssertMessage(() => {
37+
expectPowerAssertMessage(() => {
5038
assert.equal(1, minusOne);
5139
}, expected);
5240
});
@@ -57,7 +45,7 @@ describe('espower-typescript: ts', function() {
5745
| |
5846
| Object{"$$typeof":Symbol(react.element),type:"input",key:null,ref:null,props:#Object#,_owner:null,_store:#Object#}
5947
Object{default:#function#} `;
60-
this.expectPowerAssertMessage(() => {
48+
expectPowerAssertMessage(() => {
6149
assert.equal(1, MyComponent());
6250
}, expected);
6351
});

test/to_be_instrumented_test.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
'use strict';
22

3-
import assert = require('assert')
4-
import expect = require('expect.js')
3+
import assert = require('assert');
54
import React = require('react');
5+
import expectPowerAssertMessage from './lib/expectPowerAssertMessage';
66

77
describe('espower-typescript: tsx', function() {
8-
9-
beforeEach(function() {
10-
this.expectPowerAssertMessage = (body: () => void, expectedLines: string) => {
11-
try {
12-
body();
13-
expect().fail('AssertionError should be thrown');
14-
} catch(e) {
15-
expect(e.message.split('\n').slice(2, -1).join('\n')).to.eql(expectedLines);
16-
}
17-
}
18-
});
19-
208
it('jsx:react', function() {
219
let Foo = (): any => {
2210
return (<input />);
@@ -26,7 +14,7 @@ describe('espower-typescript: tsx', function() {
2614
` assert.equal(1, Foo())
2715
|
2816
Object{"$$typeof":Symbol(react.element),type:"input",key:null,ref:null,props:#Object#,_owner:null,_store:#Object#}`;
29-
this.expectPowerAssertMessage(() => {
17+
expectPowerAssertMessage(() => {
3018
assert.equal(1, Foo());
3119
}, expected);
3220
});

0 commit comments

Comments
 (0)