@@ -6,9 +6,14 @@ import 'ses';
66import test from 'ava' ;
77import path from 'path' ;
88import { scaffold } from './scaffold.js' ;
9+ import {
10+ defaultParserForLanguage ,
11+ parserForLanguageWithCjsBabel ,
12+ } from '../src/import-parsers.js' ;
913
1014/**
1115 * @import {FixtureAssertionFn} from './test.types.js';
16+ * @import {ThirdPartyStaticModuleInterface} from 'ses'
1217 */
1318
1419const fixture = new URL (
@@ -19,9 +24,13 @@ const fixtureDirname = new URL(
1924 'fixtures-cjs-compat/node_modules/app/dirname.js' ,
2025 import . meta. url ,
2126) . toString ( ) ;
27+ const fixtureDynamicImport = new URL (
28+ 'fixtures-cjs-compat/node_modules/dynamic-import/index.js' ,
29+ import . meta. url ,
30+ ) . toString ( ) ;
2231
2332const q = JSON . stringify ;
24-
33+ const { freeze } = Object ;
2534/**
2635 * @type {FixtureAssertionFn<{requireResolvePaths: string[]}> }
2736 */
@@ -70,50 +79,94 @@ const assertFixture = (t, { namespace, testCategoryHint }) => {
7079
7180const fixtureAssertionCount = 2 ;
7281
73- scaffold (
74- 'fixtures-cjs-compat' ,
75- test ,
76- fixture ,
77- assertFixture ,
78- fixtureAssertionCount ,
79- ) ;
82+ const parsersForLanguage = {
83+ default : defaultParserForLanguage ,
84+ babel : parserForLanguageWithCjsBabel ,
85+ } ;
86+
87+ for ( const [ name , parserForLanguage ] of Object . entries ( parsersForLanguage ) ) {
88+ scaffold (
89+ `fixtures-cjs-compat-${ name } ` ,
90+ test ,
91+ fixture ,
92+ assertFixture ,
93+ fixtureAssertionCount ,
94+ {
95+ parserForLanguage,
96+ } ,
97+ ) ;
8098
81- // Exit module errors are also deferred
82- scaffold (
83- 'fixtures-cjs-compat-exit-module' ,
84- test ,
85- fixture ,
86- assertFixture ,
87- fixtureAssertionCount ,
88- {
89- additionalOptions : {
90- importHook : async specifier => {
91- throw Error ( `${ q ( specifier ) } is NOT an exit module.` ) ;
99+ // Exit module errors are also deferred
100+ scaffold (
101+ `fixtures-cjs-compat-exit-module-${ name } ` ,
102+ test ,
103+ fixture ,
104+ assertFixture ,
105+ fixtureAssertionCount ,
106+ {
107+ additionalOptions : {
108+ importHook : async specifier => {
109+ throw Error ( `${ q ( specifier ) } is NOT an exit module.` ) ;
110+ } ,
92111 } ,
112+ parserForLanguage,
93113 } ,
94- } ,
95- ) ;
114+ ) ;
96115
97- scaffold (
98- 'fixtures-cjs-compat-__dirname' ,
99- test ,
100- fixtureDirname ,
101- ( t , { namespace, testCategoryHint } ) => {
102- if ( testCategoryHint === 'Location' ) {
103- const { __filename, __dirname } = namespace ;
104- t . is ( __filename , path . join ( __dirname , '/dirname.js' ) ) ;
105- t . assert ( ! __dirname . startsWith ( 'file://' ) ) ;
106- t . notRegex (
107- __dirname ,
108- / [ \\ / ] $ / ,
109- 'Expected __dirname to NOT have a trailing slash' ,
110- ) ;
111- } else {
112- const { __filename, __dirname } = namespace ;
113- t . is ( __dirname , null ) ;
114- t . is ( __filename , null ) ;
115- t . pass ( ) ;
116- }
117- } ,
118- 3 ,
119- ) ;
116+ scaffold (
117+ `fixtures-cjs-compat-__dirname-${ name } ` ,
118+ test ,
119+ fixtureDirname ,
120+ ( t , { namespace, testCategoryHint } ) => {
121+ if ( testCategoryHint === 'Location' ) {
122+ const { __filename, __dirname } = namespace ;
123+ t . is ( __filename , path . join ( __dirname , '/dirname.js' ) ) ;
124+ t . assert ( ! __dirname . startsWith ( 'file://' ) ) ;
125+ t . notRegex (
126+ __dirname ,
127+ / [ \\ / ] $ / ,
128+ 'Expected __dirname to NOT have a trailing slash' ,
129+ ) ;
130+ } else {
131+ const { __filename, __dirname } = namespace ;
132+ t . is ( __dirname , null ) ;
133+ t . is ( __filename , null ) ;
134+ t . pass ( ) ;
135+ }
136+ } ,
137+ 3 ,
138+ {
139+ parserForLanguage,
140+ } ,
141+ ) ;
142+
143+ scaffold (
144+ `fixtures-cjs-compat-dynamic-import-${ name } ` ,
145+ test ,
146+ fixtureDynamicImport ,
147+ async ( t , { namespace } ) => {
148+ const { namespace : dynamicNamespace } =
149+ // @ts -expect-error - untyped
150+ await namespace . dynamicImport ( 'a' ) ;
151+ t . is ( dynamicNamespace . foo , 'foo' ) ;
152+ } ,
153+ 1 ,
154+ {
155+ // NOTE: this should fail with parse-cjs, but not parse-cjs-babel
156+ knownFailure : name === 'default' ,
157+ parserForLanguage,
158+ additionalOptions : {
159+ importHook : async ( ) => {
160+ /** @type {ThirdPartyStaticModuleInterface } */
161+ return freeze ( {
162+ imports : [ ] ,
163+ exports : [ 'foo' ] ,
164+ execute : moduleExports => {
165+ moduleExports . foo = 'foo' ;
166+ } ,
167+ } ) ;
168+ } ,
169+ } ,
170+ } ,
171+ ) ;
172+ }
0 commit comments