Skip to content

Commit 3ae9a78

Browse files
committed
test: refactor test and add test cases
1 parent fc9f8e5 commit 3ae9a78

5 files changed

Lines changed: 109 additions & 89 deletions

File tree

packages/core/src/checker.test.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,10 @@ describe('checkCSSModule', () => {
195195
]
196196
`);
197197
});
198-
test('report diagnostics for non-existing module', async () => {
198+
test('report diagnostics for non-exported token', async () => {
199199
const iff = await createIFF({
200-
'a.module.css': dedent`
201-
@import './b.module.css';
202-
@value c_1 from './c.module.css';
203-
`,
200+
'a.module.css': `@value b_1, b_2 from './b.module.css';`,
201+
'b.module.css': `@value b_1: red;`,
204202
});
205203
const check = prepareChecker();
206204
const diagnostics = check(readAndParseCSSModule(iff.paths['a.module.css'])!);
@@ -209,69 +207,80 @@ describe('checkCSSModule', () => {
209207
{
210208
"category": "error",
211209
"fileName": "<rootDir>/a.module.css",
212-
"length": 14,
210+
"length": 3,
213211
"start": {
214-
"column": 10,
212+
"column": 13,
215213
"line": 1,
216214
},
217-
"text": "Cannot import module './b.module.css'",
218-
},
219-
{
220-
"category": "error",
221-
"fileName": "<rootDir>/a.module.css",
222-
"length": 14,
223-
"start": {
224-
"column": 18,
225-
"line": 2,
226-
},
227-
"text": "Cannot import module './c.module.css'",
215+
"text": "Module './b.module.css' has no exported token 'b_2'.",
228216
},
229217
]
230218
`);
231219
});
232-
test('report diagnostics for non-exported token', async () => {
220+
test('report diagnostics for unresolvable modules', async () => {
233221
const iff = await createIFF({
234-
'a.module.css': `@value b_1, b_2 from './b.module.css';`,
235-
'b.module.css': `@value b_1: red;`,
222+
'a.module.css': dedent`
223+
@import './b.module.css';
224+
@import 'package/c.module.css';
225+
@value b_1 from './b.module.css';
226+
@value c_1 from 'package/c.module.css';
227+
`,
236228
});
237229
const check = prepareChecker();
238230
const diagnostics = check(readAndParseCSSModule(iff.paths['a.module.css'])!);
231+
// TODO: Report diagnostics for `package/c.module.css`
239232
expect(formatDiagnostics(diagnostics, iff.rootDir)).toMatchInlineSnapshot(`
240233
[
241234
{
242235
"category": "error",
243236
"fileName": "<rootDir>/a.module.css",
244-
"length": 3,
237+
"length": 14,
245238
"start": {
246-
"column": 13,
239+
"column": 10,
247240
"line": 1,
248241
},
249-
"text": "Module './b.module.css' has no exported token 'b_2'.",
242+
"text": "Cannot import module './b.module.css'",
243+
},
244+
{
245+
"category": "error",
246+
"fileName": "<rootDir>/a.module.css",
247+
"length": 14,
248+
"start": {
249+
"column": 18,
250+
"line": 3,
251+
},
252+
"text": "Cannot import module './b.module.css'",
250253
},
251254
]
252255
`);
253256
});
254-
test('ignore token importers for unresolvable modules', async () => {
257+
test('do not report diagnostics for `@import` for URLs and unmatched modules', async () => {
255258
const iff = await createIFF({
256-
'a.module.css': `@import 'unresolvable';`,
259+
'a.module.css': dedent`
260+
@import 'https://example.com/a.module.css';
261+
@import './unmatched.module.css';
262+
`,
263+
'unmatched.module.css': '.unmatched_1 { color: red; }',
264+
});
265+
const check = prepareChecker({
266+
matchesPattern: (path) => path.endsWith('.module.css') && !path.endsWith('unmatched.module.css'),
257267
});
258-
const check = prepareChecker();
259268
const diagnostics = check(readAndParseCSSModule(iff.paths['a.module.css'])!);
260269
expect(diagnostics).toEqual([]);
261270
});
262-
test('ignore token importers that do not match the pattern', async () => {
271+
test('report diagnostics for `@value ... from ...` for URLs and unmatched modules', async () => {
263272
const iff = await createIFF({
264273
'a.module.css': dedent`
265-
@import './b.module.css';
266-
@value non_existing_value from './b.module.css';
274+
@value a_1 from 'https://example.com/a.module.css';
275+
@value unmatched_1 from './unmatched.module.css';
267276
`,
268-
'b.module.css': '.b_1 { color: red; }',
277+
'unmatched.module.css': '.unmatched_1 { color: red; }',
269278
});
270279
const check = prepareChecker({
271-
matchesPattern: (path) => path.endsWith('.module.css') && !path.endsWith('b.module.css'),
280+
matchesPattern: (path) => path.endsWith('.module.css') && !path.endsWith('unmatched.module.css'),
272281
});
273282
const diagnostics = check(readAndParseCSSModule(iff.paths['a.module.css'])!);
274-
// TODO: Report a diagnostic for code that imports values from modules that do not match the pattern.
283+
// TODO: Report diagnostics
275284
expect(diagnostics).toEqual([]);
276285
});
277286
});

packages/core/src/dts-generator.test.ts

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -94,40 +94,49 @@ describe('generateDts', () => {
9494
"
9595
`);
9696
});
97-
test('does not generate types for external files', async () => {
97+
test('does not generate `@import` types for unmatched or unresolvable modules', async () => {
9898
const iff = await createIFF({
9999
'test.module.css': dedent`
100-
@import './external.css';
101-
@value imported from './external.css';
100+
@import './unmatched.module.css';
101+
@import './unresolvable.module.css';
102102
`,
103-
'external.css': '',
103+
'unmatched.module.css': '.unmatched_1 { color: red; }',
104104
});
105+
// FIXME: Currently, the type for unresolvable modules is still generated.
105106
expect(
106107
generateDts(
107108
readAndParseCSSModule(iff.paths['test.module.css'])!,
108-
{ ...host, matchesPattern: (path) => path.endsWith('.module.css') },
109+
{ ...host, matchesPattern: (path) => path.endsWith('.module.css') && !path.endsWith('unmatched.module.css') },
109110
options,
110111
).text,
111112
).toMatchInlineSnapshot(`
112113
"// @ts-nocheck
113114
declare const styles = {
115+
...(await import('./unresolvable.module.css')).default,
114116
};
115117
export default styles;
116118
"
117119
`);
118120
});
119-
test('does not generate types for unresolved files', async () => {
121+
test('generates `@value` types for unmatched or unresolvable modules', async () => {
120122
const iff = await createIFF({
121123
'test.module.css': dedent`
122-
@import '@/a.module.css';
124+
@value unmatched_1 from './unmatched.module.css';
125+
@value unresolvable_1 from './unresolvable.module.css';
123126
`,
127+
'unmatched.module.css': '.unmatched_1 { color: red; }',
124128
});
125-
const resolver = (_specifier: string) => undefined;
129+
// FIXME: Currently, the type for unmatched modules is missing.
126130
expect(
127-
generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, { ...host, resolver }, options).text,
131+
generateDts(
132+
readAndParseCSSModule(iff.paths['test.module.css'])!,
133+
{ ...host, matchesPattern: (path) => path.endsWith('.module.css') && !path.endsWith('unmatched.module.css') },
134+
options,
135+
).text,
128136
).toMatchInlineSnapshot(`
129137
"// @ts-nocheck
130138
declare const styles = {
139+
unresolvable_1: (await import('./unresolvable.module.css')).default.unresolvable_1,
131140
};
132141
export default styles;
133142
"
@@ -145,9 +154,8 @@ describe('generateDts', () => {
145154
@value b_2: red;
146155
`,
147156
});
148-
expect(
149-
generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, host, options).text,
150-
).toMatchInlineSnapshot(`
157+
expect(generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, host, options).text)
158+
.toMatchInlineSnapshot(`
151159
"// @ts-nocheck
152160
declare const styles = {
153161
};
@@ -159,9 +167,8 @@ describe('generateDts', () => {
159167
const iff = await createIFF({
160168
'test.module.css': '.__proto__ { color: red; }',
161169
});
162-
expect(
163-
generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, host, options).text,
164-
).toMatchInlineSnapshot(`
170+
expect(generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, host, options).text)
171+
.toMatchInlineSnapshot(`
165172
"// @ts-nocheck
166173
declare const styles = {
167174
};
@@ -174,21 +181,13 @@ describe('generateDts', () => {
174181
'test.module.css': '.default { color: red; }',
175182
});
176183
expect(
177-
generateDts(
178-
readAndParseCSSModule(iff.paths['test.module.css'])!,
179-
host,
180-
{ ...options, namedExports: true },
181-
).text,
184+
generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, host, { ...options, namedExports: true }).text,
182185
).toMatchInlineSnapshot(`
183186
"// @ts-nocheck
184187
"
185188
`);
186189
expect(
187-
generateDts(
188-
readAndParseCSSModule(iff.paths['test.module.css'])!,
189-
host,
190-
{ ...options, namedExports: false },
191-
).text,
190+
generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, host, { ...options, namedExports: false }).text,
192191
).toMatchInlineSnapshot(`
193192
"// @ts-nocheck
194193
declare const styles = {
@@ -213,11 +212,7 @@ describe('generateDts', () => {
213212
`,
214213
});
215214
expect(
216-
generateDts(
217-
readAndParseCSSModule(iff.paths['test.module.css'])!,
218-
host,
219-
{ ...options, namedExports: true },
220-
).text,
215+
generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, host, { ...options, namedExports: true }).text,
221216
).toMatchInlineSnapshot(`
222217
"// @ts-nocheck
223218
export var local1: string;
@@ -235,11 +230,12 @@ describe('generateDts', () => {
235230
'test.module.css': '.local1 { color: red; }',
236231
});
237232
expect(
238-
generateDts(
239-
readAndParseCSSModule(iff.paths['test.module.css'])!,
240-
host,
241-
{ ...options, namedExports: true, forTsPlugin: true, prioritizeNamedImports: false },
242-
).text,
233+
generateDts(readAndParseCSSModule(iff.paths['test.module.css'])!, host, {
234+
...options,
235+
namedExports: true,
236+
forTsPlugin: true,
237+
prioritizeNamedImports: false,
238+
}).text,
243239
).toMatchInlineSnapshot(`
244240
"// @ts-nocheck
245241
export var local1: string;

packages/core/src/export-builder.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,37 +82,37 @@ describe('ExportBuilder', () => {
8282
}
8383
`);
8484
});
85-
test('do not collect tokens from unresolvable modules', async () => {
85+
test('do not collect tokens from `@import` for unmatched or unresolvable modules', async () => {
8686
const iff = await createIFF({
87-
'a.module.css': `@import './unresolvable.module.css';`,
87+
'a.module.css': dedent`
88+
@import './unmatched.module.css';
89+
@import './unresolvable.module.css';
90+
`,
91+
'unmatched.module.css': '.unmatched_1 { color: red; }',
8892
});
89-
const exportBuilder = prepareExportBuilder();
90-
const cssModule = readAndParseCSSModule(iff.paths['a.module.css'])!;
91-
expect(exportBuilder.build(cssModule)).toMatchInlineSnapshot(`
92-
{
93-
"allTokens": [],
94-
}
95-
`);
96-
});
97-
test('do not collect tokens from modules that do not match the pattern', async () => {
98-
const iff = await createIFF({
99-
'a.module.css': `@import './b.css';`,
100-
'b.css': '.b_1 { color: red; }',
93+
const exportBuilder = prepareExportBuilder({
94+
matchesPattern: (path) => path.endsWith('.module.css') && !path.endsWith('unmatched.module.css'),
10195
});
102-
const exportBuilder = prepareExportBuilder();
10396
const cssModule = readAndParseCSSModule(iff.paths['a.module.css'])!;
10497
expect(exportBuilder.build(cssModule)).toMatchInlineSnapshot(`
10598
{
10699
"allTokens": [],
107100
}
108101
`);
109102
});
110-
test('do not collect tokens from non-existing modules', async () => {
103+
test('collect tokens from `@value ... from ...` for unmatched or unresolvable modules', async () => {
111104
const iff = await createIFF({
112-
'a.module.css': `@import './non-existing.module.css';`,
105+
'a.module.css': dedent`
106+
@value unmatched_1 from './unmatched.module.css';
107+
@value unresolvable_1 from './unresolvable.module.css';
108+
`,
109+
'unmatched.module.css': '.unmatched_1 { color: red; }',
110+
});
111+
const exportBuilder = prepareExportBuilder({
112+
matchesPattern: (path) => path.endsWith('.module.css') && !path.endsWith('unmatched.module.css'),
113113
});
114-
const exportBuilder = prepareExportBuilder();
115114
const cssModule = readAndParseCSSModule(iff.paths['a.module.css'])!;
115+
// TODO: It should collect tokens
116116
expect(exportBuilder.build(cssModule)).toMatchInlineSnapshot(`
117117
{
118118
"allTokens": [],

packages/core/src/resolver.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe('createResolver', async () => {
3232
const resolve = createResolver(normalizeCompilerOptions({}, iff.rootDir), undefined);
3333
expect(resolve('./a.module.css', { request })).toBe(iff.paths['a.module.css']);
3434
expect(resolve('./dir/a.module.css', { request })).toBe(iff.paths['dir/a.module.css']);
35+
// FIXME: It should return `undefined`.
36+
expect(resolve('./non-existent.module.css', { request })).toBe(iff.join('non-existent.module.css'));
3537
});
3638
describe('resolve with `paths` option', () => {
3739
test('basic', () => {
@@ -103,8 +105,21 @@ describe('createResolver', async () => {
103105
expect(resolve('~package/a.module.css', { request })).toBe(undefined);
104106
});
105107
test('ignore URL', () => {
106-
const resolve = createResolver(normalizeCompilerOptions({}, iff.rootDir), undefined);
107-
expect(resolve('http://example.com/a.module.css', { request })).toBe(undefined);
108+
const resolve = createResolver(
109+
normalizeCompilerOptions(
110+
{
111+
paths: {
112+
'https://paths.com/*': ['./paths1/*'],
113+
},
114+
},
115+
iff.rootDir,
116+
),
117+
undefined,
118+
);
119+
expect(resolve('https://example.com/a.module.css', { request })).toBe(undefined);
120+
// Tests that the URL specifier is not resolved using import aliases such as paths.
121+
// FIXME: It should return `undefined`.
122+
expect(resolve('https://paths.com/a.module.css', { request })).toBe(iff.paths['paths1/a.module.css']);
108123
expect(resolve('unknown://example.com/a.module.css', { request })).toBe(undefined);
109124
expect(resolve(`data:,${encodeURIComponent('.a_1 { color: red; }')}`, { request })).toBe(undefined);
110125
});

packages/ts-plugin/e2e-test/feature/semantic-diagnostics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test('Semantic Diagnostics', async () => {
1010
import styles from './a.module.css';
1111
type Expected = { a_1: string, a_2: string, b_1: string, c_1: string, c_alias: string };
1212
const t1: Expected = styles;
13-
const t2: typeof styles = 0 as any as Expected;
13+
const t2: typeof styles = t1;
1414
styles.unknown;
1515
`,
1616
'a.module.css': dedent`

0 commit comments

Comments
 (0)