@@ -4,8 +4,29 @@ import _Ajv from "./ajv"
4
4
import standaloneCode from "../dist/standalone"
5
5
import ajvFormats from "ajv-formats"
6
6
import requireFromString = require( "require-from-string" )
7
+ import { importFromStringSync } from "module-from-string"
7
8
import assert = require( "assert" )
8
9
10
+ function testExportTypeEsm ( moduleCode : string , singleExport : boolean ) {
11
+ //Must have
12
+ assert . strictEqual ( moduleCode . includes ( "export const" ) , true )
13
+ if ( singleExport ) {
14
+ assert . strictEqual ( moduleCode . includes ( "export default" ) , true )
15
+ }
16
+ //Must not have
17
+ assert . strictEqual ( moduleCode . includes ( "module.exports" ) , false )
18
+ }
19
+ function testExportTypeCjs ( moduleCode : string , singleExport : boolean ) {
20
+ //Must have
21
+ if ( singleExport ) {
22
+ assert . strictEqual ( moduleCode . includes ( "module.exports" ) , true )
23
+ } else {
24
+ assert . strictEqual ( moduleCode . includes ( "exports." ) || moduleCode . includes ( "exports[" ) , true )
25
+ }
26
+ //Must not have
27
+ assert . strictEqual ( moduleCode . includes ( "export const" ) , false )
28
+ }
29
+
9
30
describe ( "standalone code generation" , ( ) => {
10
31
describe ( "multiple exports" , ( ) => {
11
32
let ajv : Ajv
@@ -21,31 +42,68 @@ describe("standalone code generation", () => {
21
42
}
22
43
23
44
describe ( "without schema keys" , ( ) => {
24
- beforeEach ( ( ) => {
45
+ it ( "should generate module code with named export - CJS" , ( ) => {
25
46
ajv = new _Ajv ( { code : { source : true } } )
26
47
ajv . addSchema ( numSchema )
27
48
ajv . addSchema ( strSchema )
49
+ const moduleCode = standaloneCode ( ajv , {
50
+ validateNumber : "https://example.com/number.json" ,
51
+ validateString : "https://example.com/string.json" ,
52
+ } )
53
+ testExportTypeCjs ( moduleCode , false )
54
+ const m = requireFromString ( moduleCode )
55
+ assert . strictEqual ( Object . keys ( m ) . length , 2 )
56
+ testExports ( m )
28
57
} )
29
58
30
- it ( "should generate module code with named exports" , ( ) => {
59
+ it ( "should generate module code with named export - ESM" , ( ) => {
60
+ ajv = new _Ajv ( { code : { source : true , esm : true } } )
61
+ ajv . addSchema ( numSchema )
62
+ ajv . addSchema ( strSchema )
31
63
const moduleCode = standaloneCode ( ajv , {
32
64
validateNumber : "https://example.com/number.json" ,
33
65
validateString : "https://example.com/string.json" ,
34
66
} )
35
- const m = requireFromString ( moduleCode )
67
+ testExportTypeEsm ( moduleCode , false )
68
+ const m = importFromStringSync ( moduleCode )
36
69
assert . strictEqual ( Object . keys ( m ) . length , 2 )
37
70
testExports ( m )
38
71
} )
39
72
40
- it ( "should generate module code with all exports" , ( ) => {
73
+ it ( "should generate module code with all exports - CJS" , ( ) => {
74
+ ajv = new _Ajv ( { code : { source : true } } )
75
+ ajv . addSchema ( numSchema )
76
+ ajv . addSchema ( strSchema )
41
77
const moduleCode = standaloneCode ( ajv )
78
+ testExportTypeCjs ( moduleCode , false )
42
79
const m = requireFromString ( moduleCode )
43
80
assert . strictEqual ( Object . keys ( m ) . length , 2 )
44
81
testExports ( {
45
82
validateNumber : m [ "https://example.com/number.json" ] ,
46
83
validateString : m [ "https://example.com/string.json" ] ,
47
84
} )
48
85
} )
86
+
87
+ it ( "should generate module code with all exports - ESM" , ( ) => {
88
+ ajv = new _Ajv ( { code : { source : true , esm : true } } )
89
+ ajv . addSchema ( numSchema )
90
+ ajv . addSchema ( strSchema )
91
+
92
+ try {
93
+ standaloneCode ( ajv )
94
+ } catch ( err ) {
95
+ if ( err instanceof Error ) {
96
+ const isMappingErr =
97
+ `CodeGen: invalid export name: ${ numSchema . $id } , use explicit $id name mapping` ===
98
+ err . message ||
99
+ `CodeGen: invalid export name: ${ strSchema . $id } , use explicit $id name mapping` ===
100
+ err . message
101
+ assert . strictEqual ( isMappingErr , true )
102
+ } else {
103
+ throw err
104
+ }
105
+ }
106
+ } )
49
107
} )
50
108
51
109
describe ( "with schema keys" , ( ) => {
@@ -223,13 +281,14 @@ describe("standalone code generation", () => {
223
281
}
224
282
} )
225
283
226
- it ( "should generate module code with a single export (ESM compatible) " , ( ) => {
284
+ it ( "should generate module code with a single export - CJS " , ( ) => {
227
285
const ajv = new _Ajv ( { code : { source : true } } )
228
286
const v = ajv . compile ( {
229
287
type : "number" ,
230
288
minimum : 0 ,
231
289
} )
232
290
const moduleCode = standaloneCode ( ajv , v )
291
+ testExportTypeCjs ( moduleCode , true )
233
292
const m = requireFromString ( moduleCode )
234
293
testExport ( m )
235
294
testExport ( m . default )
@@ -242,6 +301,26 @@ describe("standalone code generation", () => {
242
301
}
243
302
} )
244
303
304
+ it ( "should generate module code with a single export - ESM" , ( ) => {
305
+ const ajv = new _Ajv ( { code : { source : true , esm : true } } )
306
+ const v = ajv . compile ( {
307
+ type : "number" ,
308
+ minimum : 0 ,
309
+ } )
310
+ const moduleCode = standaloneCode ( ajv , v )
311
+ testExportTypeEsm ( moduleCode , true )
312
+ const m = importFromStringSync ( moduleCode )
313
+ testExport ( m . validate )
314
+ testExport ( m . default )
315
+
316
+ function testExport ( validate : AnyValidateFunction < unknown > ) {
317
+ assert . strictEqual ( validate ( 1 ) , true )
318
+ assert . strictEqual ( validate ( 0 ) , true )
319
+ assert . strictEqual ( validate ( - 1 ) , false )
320
+ assert . strictEqual ( validate ( "1" ) , false )
321
+ }
322
+ } )
323
+
245
324
describe ( "standalone code with ajv-formats" , ( ) => {
246
325
const schema = {
247
326
$schema : "http://json-schema.org/draft-07/schema#" ,
0 commit comments