generated from VitorLuizC/typescript-library-boilerplate
/
index.test.ts
158 lines (129 loc) · 4.36 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import resolverForTSJest, { Path, ResolverOptions } from './index.js';
import defaultResolver from 'jest-resolve/build/defaultResolver';
jest.mock('jest-resolve/build/defaultResolver');
const defaultResolverMock = (defaultResolver as unknown) as jest.Mock<
Path,
[Path, ResolverOptions]
>;
const DEFAULT_OPTIONS: ResolverOptions = {
basedir: '/usr/src/app',
defaultResolver,
};
describe('resolverForTSJest', () => {
beforeEach(() => {
defaultResolverMock.mockReset();
});
describe('when path has ".js" extension', () => {
const PATH_WITH_JS = './lib/formatToBRL.js';
const PATH_WITH_TS = './lib/formatToBRL.ts';
const PATH_WITH_TSX = './lib/formatToBRL.tsx';
describe('and the module has ".ts" extension', () => {
beforeEach(() => {
defaultResolverMock.mockImplementationOnce((path) => path);
});
it('tries to resolve path with ".ts" extension', () => {
resolverForTSJest(PATH_WITH_JS, DEFAULT_OPTIONS);
expect(defaultResolverMock).toHaveBeenCalledWith(
PATH_WITH_TS,
DEFAULT_OPTIONS,
);
});
it('resolves to the module with ".ts" extension', () => {
const result = resolverForTSJest(PATH_WITH_JS, DEFAULT_OPTIONS);
expect(result).toBe(PATH_WITH_TS);
});
});
describe('and the module has ".tsx" extension', () => {
beforeEach(() => {
defaultResolverMock
.mockImplementationOnce(() => {
throw new Error('ENOENT');
})
.mockImplementationOnce((path) => path);
});
it('tries to resolve path with ".ts" and them ".tsx" extension', () => {
resolverForTSJest(PATH_WITH_JS, DEFAULT_OPTIONS);
expect(defaultResolverMock).toHaveBeenNthCalledWith(
1,
PATH_WITH_TS,
DEFAULT_OPTIONS,
);
expect(defaultResolverMock).toHaveBeenNthCalledWith(
2,
PATH_WITH_TSX,
DEFAULT_OPTIONS,
);
});
it('resolves to the module with ".tsx" extension', () => {
const result = resolverForTSJest(PATH_WITH_JS, DEFAULT_OPTIONS);
expect(result).toBe(PATH_WITH_TSX);
});
});
describe('and there are no module with ".ts" or ".tsx" extension', () => {
beforeEach(() => {
defaultResolverMock
.mockImplementationOnce(() => {
throw new Error('ENOENT');
})
.mockImplementationOnce(() => {
throw new Error('ENOENT');
})
.mockImplementationOnce((path) => path);
});
it('tries to resolve path with ".ts", ".tsx" and them ".js" extension', () => {
resolverForTSJest(PATH_WITH_JS, DEFAULT_OPTIONS);
expect(defaultResolverMock).toHaveBeenNthCalledWith(
1,
PATH_WITH_TS,
DEFAULT_OPTIONS,
);
expect(defaultResolverMock).toHaveBeenNthCalledWith(
2,
PATH_WITH_TSX,
DEFAULT_OPTIONS,
);
expect(defaultResolverMock).toHaveBeenNthCalledWith(
3,
PATH_WITH_JS,
DEFAULT_OPTIONS,
);
});
it('resolves to the module with ".js" extension', () => {
const result = resolverForTSJest(PATH_WITH_JS, DEFAULT_OPTIONS);
expect(result).toBe(PATH_WITH_JS);
});
});
describe('and there are no module at all', () => {
beforeEach(() => {
defaultResolverMock
.mockImplementationOnce(() => {
throw new Error('ENOENT');
})
.mockImplementationOnce(() => {
throw new Error('ENOENT');
})
.mockImplementationOnce(() => {
throw new Error('ENOENT');
});
});
it('let defaultResolver throw its error', () => {
expect(() => {
resolverForTSJest(PATH_WITH_JS, DEFAULT_OPTIONS);
}).toThrowError('ENOENT');
});
});
});
describe('when path don\'t have ".js" extension', () => {
const PATH_WITHOUT_JS = 'react';
it('resolves with received path', () => {
defaultResolverMock.mockImplementationOnce((path) => path);
const result = resolverForTSJest(PATH_WITHOUT_JS, DEFAULT_OPTIONS);
expect(defaultResolverMock).toBeCalledTimes(1);
expect(defaultResolverMock).toBeCalledWith(
PATH_WITHOUT_JS,
DEFAULT_OPTIONS,
);
expect(result).toBe(PATH_WITHOUT_JS);
});
});
});