Skip to content

Commit 0b18b8c

Browse files
committed
test: simplify path match
1 parent 55a94c7 commit 0b18b8c

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

__tests__/cjs/import.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,17 @@ export default code
136136
it('should use relative filename in error stack trace', async () => {
137137
expect.assertions(1)
138138
const filename = 'foo.js'
139-
const relativeDirname = path.relative(process.cwd(), __dirname)
140-
const relativeFilename = path.join(relativeDirname, filename)
139+
const relativeDirnamePath = path.relative(process.cwd(), __dirname)
140+
const relativeFilenamePath = path.join(relativeDirnamePath, filename)
141+
const relativeFilename = normalizePath(relativeFilenamePath)
141142
try {
142143
await importFromStringFn('throw new Error("boom")', {
143144
filename,
144145
useCurrentGlobal: true
145146
})
146147
} catch (err) {
147148
if (err instanceof Error) {
148-
expect(err.stack).toMatch(new RegExp(`at \\S*${relativeFilename}:\\d+:\\d+$`, 'm'))
149+
expect(err.stack).toMatch(`${relativeFilename}:`)
149150
} else {
150151
throw err
151152
}
@@ -163,7 +164,7 @@ export default code
163164
})
164165
} catch (err) {
165166
if (err instanceof Error) {
166-
expect(err.stack).toMatch(new RegExp(`at ${filename}:\\d+:\\d+$`, 'm'))
167+
expect(err.stack).toMatch(`${filename}:`)
167168
} else {
168169
throw err
169170
}

__tests__/cjs/require.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ it('should be able to override current global', () => {
9292
it('should use relative filename in error stack trace', () => {
9393
expect.assertions(1)
9494
const filename = 'foo.js'
95-
const relativeDirname = path.relative(process.cwd(), __dirname)
96-
const relativeFilename = path.join(relativeDirname, filename)
95+
const relativeDirnamePath = path.relative(process.cwd(), __dirname)
96+
const relativeFilenamePath = path.join(relativeDirnamePath, filename)
97+
const relativeFilename = normalizePath(relativeFilenamePath)
9798
try {
9899
requireFromString('throw new Error("boom")', {
99100
filename,
100101
useCurrentGlobal: true
101102
})
102103
} catch (err) {
103104
if (err instanceof Error) {
104-
expect(err.stack).toMatch(new RegExp(`at \\S*${relativeFilename}:\\d+:\\d+$`, 'm'))
105+
expect(err.stack).toMatch(`${relativeFilename}:`)
105106
} else {
106107
throw err
107108
}
@@ -119,7 +120,7 @@ it('should use absolute filename in error stack trace', () => {
119120
})
120121
} catch (err) {
121122
if (err instanceof Error) {
122-
expect(err.stack).toMatch(new RegExp(`at ${filename}:\\d+:\\d+$`, 'm'))
123+
expect(err.stack).toMatch(`${filename}:`)
123124
} else {
124125
throw err
125126
}

__tests__/esm/import.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os from 'os'
22
import path from 'path'
33
import { fileURLToPath, pathToFileURL } from 'url'
4+
import normalizePath from 'normalize-path'
45
import { importFromString, createImportFromString, importFromStringSync } from '../../src/index'
56

67
const __dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -139,16 +140,17 @@ export default code
139140
it('should use relative filename in error stack trace', async () => {
140141
expect.assertions(1)
141142
const filename = 'foo.js'
142-
const relativeDirname = path.relative(process.cwd(), __dirname)
143-
const relativeFilename = path.join(relativeDirname, filename)
143+
const relativeDirnamePath = path.relative(process.cwd(), __dirname)
144+
const relativeFilenamePath = path.join(relativeDirnamePath, filename)
145+
const relativeFilename = normalizePath(relativeFilenamePath)
144146
try {
145147
await importFromString('throw new Error("boom")', {
146148
filename,
147149
useCurrentGlobal: true
148150
})
149151
} catch (err) {
150152
if (err instanceof Error) {
151-
expect(err.stack).toMatch(new RegExp(`at file://\\S+${relativeFilename}:\\d+:\\d+$`, 'm'))
153+
expect(err.stack).toMatch(`${relativeFilename}:`)
152154
} else {
153155
throw err
154156
}
@@ -166,7 +168,7 @@ export default code
166168
})
167169
} catch (err) {
168170
if (err instanceof Error) {
169-
expect(err.stack).toMatch(new RegExp(`at ${filename}:\\d+:\\d+$`, 'm'))
171+
expect(err.stack).toMatch(`${filename}:`)
170172
} else {
171173
throw err
172174
}

__tests__/esm/require.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ it('should work with current global', () => {
8686
it('should use relative filename in error stack trace', () => {
8787
expect.assertions(1)
8888
const filename = 'foo.js'
89-
const relativeDirname = path.relative(process.cwd(), __dirname)
90-
const relativeFilename = path.join(relativeDirname, filename)
89+
const relativeDirnamePath = path.relative(process.cwd(), __dirname)
90+
const relativeFilenamePath = path.join(relativeDirnamePath, filename)
91+
const relativeFilename = normalizePath(relativeFilenamePath)
9192
try {
9293
requireFromString('throw new Error("boom")', {
9394
filename,
9495
useCurrentGlobal: true
9596
})
9697
} catch (err) {
9798
if (err instanceof Error) {
98-
expect(err.stack).toMatch(new RegExp(`at \\S*${relativeFilename}:\\d+:\\d+$`, 'm'))
99+
expect(err.stack).toMatch(`${relativeFilename}:`)
99100
} else {
100101
throw err
101102
}
@@ -113,7 +114,7 @@ it('should use absolute filename in error stack trace', () => {
113114
})
114115
} catch (err) {
115116
if (err instanceof Error) {
116-
expect(err.stack).toMatch(new RegExp(`at ${filename}:\\d+:\\d+$`, 'm'))
117+
expect(err.stack).toMatch(`${filename}:`)
117118
} else {
118119
throw err
119120
}

0 commit comments

Comments
 (0)