-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.test.ts
119 lines (105 loc) · 4.94 KB
/
error.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { useDangerousUDFHelpers } from '../';
import { AlreadyImplementedError, NotFoundError } from './../errors';
import { clearAllUDFHelpers } from '../';
import printer from './printer';
import { inputFixturePath } from './utils';
beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation();
jest.spyOn(console, 'warn').mockImplementation();
});
afterEach(() => {
jest.clearAllMocks();
clearAllUDFHelpers();
});
describe('addUDFHelper', () => {
describe('error', () => {
const type = 'error';
describe('core_ext', () => {
describe('babelImplemented', () => {
// prettier-ignore
const cases = [
{
method: 'BabelFile#addUDFHelper',
preFunc: (pass, helpers) => {
try {
pass.file.constructor.prototype.addUDFHelper = function () { };
useDangerousUDFHelpers(pass, helpers);
} catch (e) {
pass.file.constructor.prototype.addUDFHelper = undefined;
throw e
}
}
},
{
method: 'BabelFile#listUDFHelper',
preFunc: (pass, helpers) => {
try {
pass.file.constructor.prototype.listUDFHelper = function () { };
useDangerousUDFHelpers(pass, helpers);
} catch (e) {
pass.file.constructor.prototype.listUDFHelper = undefined;
throw e
}
}
},
];
for (const c of cases) {
const { method, preFunc } = c;
test(method, () => {
const dir = 'declaration';
const helpers = require(inputFixturePath(['basic', dir]));
const programFuncs = [(pass) => pass.addUDFHelper('declaration')];
// prettier-ignore
expect(() => { printer({ helpers, programFuncs, preFunc }); }).toThrowError(new AlreadyImplementedError(
`unknown:\
\nThis tool cannot be used. officially supported.
Please see the official documentation.
https://babeljs.io/docs/en/babel-helpers
`));
// prettier-ignore
expect(() => { printer({ helpers, programFuncs, preFunc }); }).toThrowErrorMatchingSnapshot();
});
}
});
test('Not found UDF helpers', () => {
const programFuncs = [(pass) => pass.addUDFHelper('objectWithoutProperties')];
// prettier-ignore
expect(() => { printer({ programFuncs } as any); }).toThrowError(new NotFoundError('unknown: Not found UDF helpers.'));
// prettier-ignore
expect(() => { printer({ programFuncs } as any); }).toThrowErrorMatchingSnapshot();
});
test('babelAlreadyDefined', () => {
const dir = 'babelAlreadyDefined';
const helpers = require(inputFixturePath([type, dir]));
const programFuncs = [(pass) => pass.addUDFHelper('objectWithoutProperties')];
// prettier-ignore
expect(() => { printer({ helpers, programFuncs }); }).toThrowError(new AlreadyImplementedError('unknown: objectWithoutProperties cannot be used because it is supported by babel official.\nPlease change the name of the helper.'));
// prettier-ignore
expect(() => { printer({ helpers, programFuncs }); }).toThrowErrorMatchingSnapshot();
});
});
describe('DependencyResolvePlugin', () => {
// prettier-ignore
const cases = [
{ dir: 'doNotExist', err: new ReferenceError('unknown: Unknown UDF helper doNotExist') },
{ dir: 'importNamed', err: new SyntaxError('unknown: UDF Helpers can only import a default value (This is an error on an internal node. Probably an internal error.)') },
{ dir: 'doNotGiveName', err: new SyntaxError('unknown: UDF Helpers should give names to their exported func declaration (This is an error on an internal node. Probably an internal error.)') },
{ dir: 'expression', err: new SyntaxError('unknown: UDF Helpers must be a function declaration (This is an error on an internal node. Probably an internal error.)') },
{ dir: 'exportAll', err: new SyntaxError('unknown: UDF Helpers can only export default (This is an error on an internal node. Probably an internal error.)') },
{ dir: 'exportNamed', err: new SyntaxError('unknown: UDF Helpers can only export default (This is an error on an internal node. Probably an internal error.)') },
{ dir: 'doNotExportDefault', err: new SyntaxError('unknown: UDF Helpers must default-export something.') },
];
for (const c of cases) {
const { dir, err } = c;
test(dir, () => {
const helpers = require(inputFixturePath([type, dir]));
const programFuncs = [(pass) => pass.addUDFHelper(dir)];
// prettier-ignore
expect(() => { printer({ helpers, programFuncs }); }).toThrowError(err);
// prettier-ignore
expect(() => { printer({ helpers, programFuncs }); }).toThrowErrorMatchingSnapshot();
});
}
});
});
});