|
| 1 | +import { access, chmod } from 'node:fs/promises'; |
| 2 | +import { TsConfigFileNotFoundError } from '@css-modules-kit/core'; |
| 3 | +import { describe, expect, test } from 'vitest'; |
| 4 | +import { ReadCSSModuleFileError } from './error.js'; |
| 5 | +import { createProject } from './project.js'; |
| 6 | +import { formatDiagnostics } from './test/diagnostic.js'; |
| 7 | +import { createIFF } from './test/fixture.js'; |
| 8 | + |
| 9 | +describe('createProject', () => { |
| 10 | + test('creates project', async () => { |
| 11 | + const iff = await createIFF({ |
| 12 | + 'tsconfig.json': '{}', |
| 13 | + }); |
| 14 | + const project = createProject({ project: iff.rootDir }); |
| 15 | + expect(project.config.dtsOutDir).toContain('generated'); |
| 16 | + }); |
| 17 | + test('throws TsConfigFileNotFoundError when tsconfig.json does not exist', async () => { |
| 18 | + const iff = await createIFF({}); |
| 19 | + expect(() => createProject({ project: iff.rootDir })).toThrow(TsConfigFileNotFoundError); |
| 20 | + }); |
| 21 | + test.runIf(process.platform !== 'win32')( |
| 22 | + 'throws ReadCSSModuleFileError when a CSS module file cannot be read', |
| 23 | + async () => { |
| 24 | + const iff = await createIFF({ |
| 25 | + 'tsconfig.json': '{}', |
| 26 | + 'src/a.module.css': '.a1 { color: red; }', |
| 27 | + }); |
| 28 | + await chmod(iff.paths['src/a.module.css'], 0o200); // Remove read permission |
| 29 | + expect(() => createProject({ project: iff.rootDir })).toThrow(ReadCSSModuleFileError); |
| 30 | + }, |
| 31 | + ); |
| 32 | +}); |
| 33 | + |
| 34 | +describe('getDiagnostics', () => { |
| 35 | + test('returns empty array when no diagnostics', async () => { |
| 36 | + const iff = await createIFF({ |
| 37 | + 'tsconfig.json': '{}', |
| 38 | + 'src/a.module.css': '.a_1 { color: red; }', |
| 39 | + }); |
| 40 | + const project = createProject({ project: iff.rootDir }); |
| 41 | + const diagnostics = project.getDiagnostics(); |
| 42 | + expect(diagnostics).toEqual([]); |
| 43 | + }); |
| 44 | + test('returns project diagnostics', async () => { |
| 45 | + const iff = await createIFF({ |
| 46 | + 'tsconfig.json': '{ "cmkOptions": { "dtsOutDir": 1 } }', |
| 47 | + }); |
| 48 | + const project = createProject({ project: iff.rootDir }); |
| 49 | + const diagnostics = project.getDiagnostics(); |
| 50 | + expect(formatDiagnostics(diagnostics, iff.rootDir)).toMatchInlineSnapshot(` |
| 51 | + [ |
| 52 | + { |
| 53 | + "category": "error", |
| 54 | + "text": "\`dtsOutDir\` in <rootDir>/tsconfig.json must be a string.", |
| 55 | + }, |
| 56 | + { |
| 57 | + "category": "error", |
| 58 | + "text": "The file specified in tsconfig.json not found.", |
| 59 | + }, |
| 60 | + ] |
| 61 | + `); |
| 62 | + }); |
| 63 | + test('returns syntactic diagnostics', async () => { |
| 64 | + const iff = await createIFF({ |
| 65 | + 'tsconfig.json': '{}', |
| 66 | + 'src/a.module.css': '.a_1 {', |
| 67 | + 'src/b.module.css': '.a_2 { color }', |
| 68 | + }); |
| 69 | + const project = createProject({ project: iff.rootDir }); |
| 70 | + const diagnostics = project.getDiagnostics(); |
| 71 | + expect(formatDiagnostics(diagnostics, iff.rootDir)).toMatchInlineSnapshot(` |
| 72 | + [ |
| 73 | + { |
| 74 | + "category": "error", |
| 75 | + "fileName": "<rootDir>/src/a.module.css", |
| 76 | + "length": 1, |
| 77 | + "start": { |
| 78 | + "column": 1, |
| 79 | + "line": 1, |
| 80 | + }, |
| 81 | + "text": "Unclosed block", |
| 82 | + }, |
| 83 | + { |
| 84 | + "category": "error", |
| 85 | + "fileName": "<rootDir>/src/b.module.css", |
| 86 | + "length": 5, |
| 87 | + "start": { |
| 88 | + "column": 8, |
| 89 | + "line": 1, |
| 90 | + }, |
| 91 | + "text": "Unknown word color", |
| 92 | + }, |
| 93 | + ] |
| 94 | + `); |
| 95 | + }); |
| 96 | + |
| 97 | + test('returns semantic diagnostics', async () => { |
| 98 | + const iff = await createIFF({ |
| 99 | + 'tsconfig.json': '{}', |
| 100 | + 'src/a.module.css': `@import './non-existent-1.module.css';`, |
| 101 | + 'src/b.module.css': `@import './non-existent-2.module.css';`, |
| 102 | + }); |
| 103 | + const project = createProject({ project: iff.rootDir }); |
| 104 | + const diagnostics = project.getDiagnostics(); |
| 105 | + expect(formatDiagnostics(diagnostics, iff.rootDir)).toMatchInlineSnapshot(` |
| 106 | + [ |
| 107 | + { |
| 108 | + "category": "error", |
| 109 | + "fileName": "<rootDir>/src/a.module.css", |
| 110 | + "length": 27, |
| 111 | + "start": { |
| 112 | + "column": 10, |
| 113 | + "line": 1, |
| 114 | + }, |
| 115 | + "text": "Cannot import module './non-existent-1.module.css'", |
| 116 | + }, |
| 117 | + { |
| 118 | + "category": "error", |
| 119 | + "fileName": "<rootDir>/src/b.module.css", |
| 120 | + "length": 27, |
| 121 | + "start": { |
| 122 | + "column": 10, |
| 123 | + "line": 1, |
| 124 | + }, |
| 125 | + "text": "Cannot import module './non-existent-2.module.css'", |
| 126 | + }, |
| 127 | + ] |
| 128 | + `); |
| 129 | + }); |
| 130 | + test('skips semantic diagnostics when project or syntactic diagnostics exist', async () => { |
| 131 | + const iff = await createIFF({ |
| 132 | + 'tsconfig.json': '{ "cmkOptions": { "dtsOutDir": 1 } }', |
| 133 | + 'src/a.module.css': '.a_1 {', |
| 134 | + 'src/b.module.css': `@import './non-existent.module.css';`, |
| 135 | + }); |
| 136 | + const project = createProject({ project: iff.rootDir }); |
| 137 | + const diagnostics = project.getDiagnostics(); |
| 138 | + expect(formatDiagnostics(diagnostics, iff.rootDir)).toMatchInlineSnapshot(` |
| 139 | + [ |
| 140 | + { |
| 141 | + "category": "error", |
| 142 | + "text": "\`dtsOutDir\` in <rootDir>/tsconfig.json must be a string.", |
| 143 | + }, |
| 144 | + { |
| 145 | + "category": "error", |
| 146 | + "fileName": "<rootDir>/src/a.module.css", |
| 147 | + "length": 1, |
| 148 | + "start": { |
| 149 | + "column": 1, |
| 150 | + "line": 1, |
| 151 | + }, |
| 152 | + "text": "Unclosed block", |
| 153 | + }, |
| 154 | + ] |
| 155 | + `); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +describe('emitDtsFiles', () => { |
| 160 | + test('emits .d.ts files', async () => { |
| 161 | + const iff = await createIFF({ |
| 162 | + 'tsconfig.json': '{}', |
| 163 | + 'src/a.module.css': '.a1 { color: red; }', |
| 164 | + 'src/b.module.css': '.b1 { color: blue; }', |
| 165 | + }); |
| 166 | + const project = createProject({ project: iff.rootDir }); |
| 167 | + await project.emitDtsFiles(); |
| 168 | + expect(await iff.readFile('generated/src/a.module.css.d.ts')).toMatchInlineSnapshot(` |
| 169 | + "// @ts-nocheck |
| 170 | + declare const styles = { |
| 171 | + a1: '' as readonly string, |
| 172 | + }; |
| 173 | + export default styles; |
| 174 | + " |
| 175 | + `); |
| 176 | + expect(await iff.readFile('generated/src/b.module.css.d.ts')).toMatchInlineSnapshot(` |
| 177 | + "// @ts-nocheck |
| 178 | + declare const styles = { |
| 179 | + b1: '' as readonly string, |
| 180 | + }; |
| 181 | + export default styles; |
| 182 | + " |
| 183 | + `); |
| 184 | + }); |
| 185 | + test('does not emit .d.ts files for files not matched by `pattern`', async () => { |
| 186 | + const iff = await createIFF({ |
| 187 | + 'tsconfig.json': '{}', |
| 188 | + 'src/a.module.css': '.a1 { color: red; }', |
| 189 | + 'src/b.css': '.b1 { color: blue; }', |
| 190 | + }); |
| 191 | + const project = createProject({ project: iff.rootDir }); |
| 192 | + await project.emitDtsFiles(); |
| 193 | + expect(await iff.readFile('generated/src/a.module.css.d.ts')).toMatchInlineSnapshot(` |
| 194 | + "// @ts-nocheck |
| 195 | + declare const styles = { |
| 196 | + a1: '' as readonly string, |
| 197 | + }; |
| 198 | + export default styles; |
| 199 | + " |
| 200 | + `); |
| 201 | + await expect(access(iff.join('generated/src/a.module.css.d.ts'))).resolves.not.toThrow(); |
| 202 | + await expect(access(iff.join('generated/src/b.css.d.ts'))).rejects.toThrow(); |
| 203 | + }); |
| 204 | +}); |
0 commit comments