1
- // @ts -nocheck
2
1
import path from 'path' ;
3
- import fs from 'fs' ;
4
- import { format } from 'prettier' ;
5
2
import { fileURLToPath } from 'url' ;
6
3
import { test } from 'uvu' ;
7
4
import * as assert from 'uvu/assert' ;
8
- import { rimraf , walk } from '../../../utils/filesystem.js' ;
5
+ import { rimraf } from '../../../utils/filesystem.js' ;
9
6
import options from '../../config/options.js' ;
10
7
import create_manifest_data from '../create_manifest_data/index.js' ;
11
8
import { tweak_types , write_all_types } from './index.js' ;
9
+ import { execSync } from 'child_process' ;
12
10
13
11
const cwd = fileURLToPath ( new URL ( './test' , import . meta. url ) ) ;
14
12
15
- function format_dts ( file ) {
16
- // format with the same settings we use in this monorepo so
17
- // the output is the same as visible when opening the $types.d.ts
18
- // files in the editor
19
- return format ( fs . readFileSync ( file , 'utf-8' ) , {
20
- parser : 'typescript' ,
21
- useTabs : true ,
22
- singleQuote : true ,
23
- trailingComma : 'none' ,
24
- printWidth : 100
25
- } ) ;
26
- }
27
-
28
13
/**
29
14
* @param {string } dir
30
- * @param {(code: string) => string } [prepare_expected]
31
15
*/
32
- async function run_test ( dir , prepare_expected = ( code ) => code ) {
16
+ async function run_test ( dir ) {
33
17
rimraf ( path . join ( cwd , dir , '.svelte-kit' ) ) ;
34
18
35
19
const initial = options ( { } , 'config' ) ;
@@ -43,66 +27,25 @@ async function run_test(dir, prepare_expected = (code) => code) {
43
27
config : /** @type {import('types').ValidatedConfig } */ ( initial )
44
28
} ) ;
45
29
await write_all_types ( initial , manifest ) ;
46
-
47
- const expected_dir = path . join ( cwd , dir , '_expected' ) ;
48
- const expected_files = walk ( expected_dir , true ) ;
49
- const actual_dir = path . join (
50
- path . join ( cwd , dir , '.svelte-kit' , 'types' ) ,
51
- path . relative ( process . cwd ( ) , path . join ( cwd , dir ) )
52
- ) ;
53
- const actual_files = walk ( actual_dir , true ) ;
54
-
55
- assert . equal ( actual_files , expected_files ) ;
56
-
57
- for ( const file of actual_files ) {
58
- const expected_file = path . join ( expected_dir , file ) ;
59
- const actual_file = path . join ( actual_dir , file ) ;
60
- if ( fs . statSync ( path . join ( actual_dir , file ) ) . isDirectory ( ) ) {
61
- assert . ok ( fs . statSync ( actual_file ) . isDirectory ( ) , 'Expected a directory' ) ;
62
- continue ;
63
- }
64
-
65
- const expected = format_dts ( expected_file ) ;
66
- const actual = format_dts ( actual_file ) ;
67
- const err_msg = `Expected equal file contents for ${ file } in ${ dir } ` ;
68
- assert . fixture ( actual , prepare_expected ( expected ) , err_msg ) ;
69
- }
70
30
}
71
31
72
- test ( 'Create $types for +page.js' , async ( ) => {
32
+ test ( 'Creates correct $types' , async ( ) => {
33
+ // To safe us from creating a real SvelteKit project for each of the tests,
34
+ // we first run the type generation directly for each test case, and then
35
+ // call `tsc` to check that the generated types are valid.
73
36
await run_test ( 'simple-page-shared-only' ) ;
74
- } ) ;
75
-
76
- test ( 'Create $types for page.server.js' , async ( ) => {
77
37
await run_test ( 'simple-page-server-only' ) ;
78
- } ) ;
79
-
80
- test ( 'Create $types for page(.server).js' , async ( ) => {
81
38
await run_test ( 'simple-page-server-and-shared' ) ;
82
- } ) ;
83
-
84
- test ( 'Create $types for layout and page' , async ( ) => {
85
39
await run_test ( 'layout' ) ;
86
- } ) ;
87
-
88
- test ( 'Create $types for grouped layout and page' , async ( ) => {
89
40
await run_test ( 'layout-advanced' ) ;
90
- } ) ;
91
-
92
- test ( 'Create $types with params' , async ( ) => {
93
- await run_test ( 'slugs' , ( code ) =>
94
- // For some reason, the order of the params differentiates between windows and mac/linux
95
- process . platform === 'win32'
96
- ? code . replace (
97
- 'rest?: string; slug?: string; optional?: string' ,
98
- 'optional?: string; rest?: string; slug?: string'
99
- )
100
- : code
101
- ) ;
102
- } ) ;
103
-
104
- test ( 'Create $types with params and required return types for layout' , async ( ) => {
41
+ await run_test ( 'slugs' ) ;
105
42
await run_test ( 'slugs-layout-not-all-pages-have-load' ) ;
43
+ try {
44
+ execSync ( 'pnpm testtypes' , { cwd } ) ;
45
+ } catch ( e ) {
46
+ console . error ( /** @type {any } */ ( e ) . stdout . toString ( ) ) ;
47
+ throw new Error ( 'Type tests failed' ) ;
48
+ }
106
49
} ) ;
107
50
108
51
test ( 'Rewrites types for a TypeScript module' , ( ) => {
0 commit comments