Skip to content

Commit 52fa582

Browse files
committed
test: add test for file operation
1 parent 3469ee0 commit 52fa582

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import { writeFile } from 'node:fs/promises';
2+
import dedent from 'dedent';
3+
import { expect, test } from 'vitest';
4+
import { createIFF } from './test-util/fixture.js';
5+
import { launchTsserver } from './test-util/tsserver.js';
6+
7+
test.fails('adding file', async () => {
8+
const tsserver = launchTsserver();
9+
const iff = await createIFF({
10+
'index.ts': dedent`
11+
import styles from './a.module.css';
12+
styles.a_1;
13+
`,
14+
'tsconfig.json': dedent`
15+
{
16+
"compilerOptions": {},
17+
"cmkOptions": {}
18+
}
19+
`,
20+
});
21+
await tsserver.sendUpdateOpen({
22+
openFiles: [{ file: iff.paths['index.ts'] }],
23+
});
24+
25+
// If a.module.css does not exist, a diagnostic should be reported in index.ts
26+
const res1 = await tsserver.sendSemanticDiagnosticsSync({
27+
file: iff.paths['index.ts'],
28+
});
29+
expect(res1.body).toMatchInlineSnapshot(`
30+
[
31+
{
32+
"category": "error",
33+
"code": 2307,
34+
"end": {
35+
"line": 1,
36+
"offset": 36,
37+
},
38+
"start": {
39+
"line": 1,
40+
"offset": 20,
41+
},
42+
"text": "Cannot find module './a.module.css' or its corresponding type declarations.",
43+
},
44+
]
45+
`);
46+
47+
// Add a.module.css
48+
await writeFile(iff.join('a.module.css'), '.a_1 { color: red; }');
49+
await tsserver.sendUpdateOpen({
50+
openFiles: [{ file: iff.join('a.module.css') }],
51+
});
52+
53+
// If a.module.css exists, the diagnostic should disappear
54+
// TODO: Fix the error being thrown
55+
const res2 = await tsserver.sendSemanticDiagnosticsSync({
56+
file: iff.paths['index.ts'],
57+
});
58+
expect(res2.body).toMatchInlineSnapshot(`[]`);
59+
});
60+
61+
test('updating file', async () => {
62+
const tsserver = launchTsserver();
63+
const iff = await createIFF({
64+
'index.ts': dedent`
65+
import styles from './a.module.css';
66+
styles.a_1;
67+
`,
68+
'a.module.css': '',
69+
'tsconfig.json': dedent`
70+
{
71+
"compilerOptions": {},
72+
"cmkOptions": {}
73+
}
74+
`,
75+
});
76+
await tsserver.sendUpdateOpen({
77+
openFiles: [{ file: iff.paths['index.ts'] }],
78+
});
79+
80+
const res1 = await tsserver.sendSemanticDiagnosticsSync({
81+
file: iff.paths['index.ts'],
82+
});
83+
expect(res1.body).toMatchInlineSnapshot(`
84+
[
85+
{
86+
"category": "error",
87+
"code": 2339,
88+
"end": {
89+
"line": 2,
90+
"offset": 11,
91+
},
92+
"start": {
93+
"line": 2,
94+
"offset": 8,
95+
},
96+
"text": "Property 'a_1' does not exist on type '{}'.",
97+
},
98+
]
99+
`);
100+
101+
// Update a.module.css to have a semantic error
102+
await writeFile(
103+
iff.paths['a.module.css'],
104+
dedent`
105+
.a_1 {}
106+
.a-2 {}
107+
`,
108+
);
109+
await tsserver.sendUpdateOpen({
110+
openFiles: [{ file: iff.paths['a.module.css'] }],
111+
});
112+
113+
// The diagnostics in a.module.css are updated
114+
const res2 = await tsserver.sendSemanticDiagnosticsSync({
115+
file: iff.paths['a.module.css'],
116+
});
117+
expect(res2.body).toMatchInlineSnapshot(`
118+
[
119+
{
120+
"category": "error",
121+
"code": 0,
122+
"end": {
123+
"line": 2,
124+
"offset": 5,
125+
},
126+
"source": "css-modules-kit",
127+
"start": {
128+
"line": 2,
129+
"offset": 2,
130+
},
131+
"text": "css-modules-kit does not support invalid names as JavaScript identifiers.",
132+
},
133+
]
134+
`);
135+
136+
// The diagnostics of files importing a.module.css are updated.
137+
const res3 = await tsserver.sendSemanticDiagnosticsSync({
138+
file: iff.paths['index.ts'],
139+
});
140+
expect(res3.body).toMatchInlineSnapshot(`[]`);
141+
});
142+
143+
test.todo('removing file');

0 commit comments

Comments
 (0)