Skip to content

Commit 784cf3e

Browse files
committed
fix esbuild cwd
1 parent 4cb88c6 commit 784cf3e

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

integration-tests/esbuild/index.spec.js

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,179 +5,182 @@
55
'use strict'
66

77
const chproc = require('child_process')
8-
const path = require('path')
8+
const pathModule = require('path')
99
const fs = require('fs')
1010
// TODO: It shouldn't be necessary to disable n/no-extraneous-require - Research
1111
// eslint-disable-next-line n/no-extraneous-require
1212
const { assert } = require('chai')
1313

14-
const TEST_DIR = path.join(__dirname, '.')
15-
process.chdir(TEST_DIR)
14+
// sub process must be executed inside TEST_DIR
15+
const TEST_DIR = pathModule.join(__dirname, '.')
16+
const execSync = (command, options) => chproc.execSync(command, { ...(options ?? {}), cwd: TEST_DIR })
17+
const rmSync = (path, options) => fs.rmSync(pathModule.join(TEST_DIR, path), options)
18+
const readFileSync = (path, options) => fs.readFileSync(pathModule.join(TEST_DIR, path), options)
1619

1720
// This should switch to our withVersion helper. The order here currently matters.
1821
const esbuildVersions = ['latest', '0.16.12']
1922

2023
esbuildVersions.forEach((version) => {
2124
describe(`esbuild ${version}`, () => {
2225
before(() => {
23-
chproc.execSync('npm install', {
26+
execSync('npm install', {
2427
timeout: 1000 * 30
2528
})
2629
if (version !== 'latest') {
27-
chproc.execSync(`npm install esbuild@${version}`, {
30+
execSync(`npm install esbuild@${version}`, {
2831
timeout: 1000 * 30
2932
})
3033
}
3134
})
3235

3336
it('works', () => {
3437
console.log('npm run build')
35-
chproc.execSync('npm run build')
38+
execSync('npm run build')
3639

3740
console.log('npm run built')
3841
try {
39-
chproc.execSync('npm run built', {
42+
execSync('npm run built', {
4043
timeout: 1000 * 30
4144
})
4245
} catch (err) {
4346
console.error(err)
4447
process.exit(1)
4548
} finally {
46-
fs.rmSync('./out.js', { force: true })
49+
rmSync('./out.js', { force: true })
4750
}
4851
})
4952

5053
it('does not bundle modules listed in .external', () => {
5154
const command = 'node ./build-and-test-skip-external.js'
5255
console.log(command)
53-
chproc.execSync(command, {
56+
execSync(command, {
5457
timeout: 1000 * 30
5558
})
5659
})
5760

5861
it('handles typescript apps that import without file extensions', () => {
5962
const command = 'node ./build-and-test-typescript.mjs'
6063
console.log(command)
61-
chproc.execSync(command, {
64+
execSync(command, {
6265
timeout: 1000 * 30
6366
})
6467
})
6568

6669
it('handles the complex aws-sdk package with dynamic requires', () => {
6770
const command = 'node ./build-and-test-aws-sdk.js'
6871
console.log(command)
69-
chproc.execSync(command, {
72+
execSync(command, {
7073
timeout: 1000 * 30
7174
})
7275
})
7376

7477
it('handles scoped node_modules', () => {
7578
const command = 'node ./build-and-test-koa.mjs'
7679
console.log(command)
77-
chproc.execSync(command, {
80+
execSync(command, {
7881
timeout: 1000 * 30
7982
})
8083
})
8184

8285
it('handles instrumentations where the patching function is a property of the hook', () => {
8386
const command = 'node ./build-and-test-openai.js'
8487
console.log(command)
85-
chproc.execSync(command, {
88+
execSync(command, {
8689
timeout: 1000 * 30
8790
})
8891
})
8992

9093
it('injects Git metadata into bundled applications', () => {
9194
const command = 'node ./build-and-test-git-tags.js'
9295
console.log(command)
93-
chproc.execSync(command, {
96+
execSync(command, {
9497
timeout: 1000 * 30
9598
})
9699
})
97100

98101
describe('ESM', () => {
99102
afterEach(() => {
100-
fs.rmSync('./out.mjs', { force: true })
101-
fs.rmSync('./out.js', { force: true })
102-
fs.rmSync('./basic-test.mjs', { force: true })
103+
rmSync('./out.mjs', { force: true })
104+
rmSync('./out.js', { force: true })
105+
rmSync('./basic-test.mjs', { force: true })
103106
})
104107

105108
it('works', () => {
106109
console.log('npm run build:esm')
107-
chproc.execSync('npm run build:esm')
110+
execSync('npm run build:esm')
108111
console.log('npm run built:esm')
109-
chproc.execSync('npm run built:esm', {
112+
execSync('npm run built:esm', {
110113
timeout: 1000 * 30
111114
})
112115
})
113116

114117
it('should not override existing js banner', () => {
115118
const command = 'node ./build-and-run.esm-unrelated-js-banner.mjs'
116119
console.log(command)
117-
chproc.execSync(command, {
120+
execSync(command, {
118121
timeout: 1000 * 30
119122
})
120123

121-
const builtFile = fs.readFileSync('./out.mjs').toString()
124+
const builtFile = readFileSync('./out.mjs').toString()
122125
assert.include(builtFile, '/* js test */')
123126
})
124127

125128
it('should contain the definitions when esm is inferred from outfile', () => {
126129
const command = 'node ./build-and-run.esm-relying-in-extension.mjs'
127130
console.log(command)
128-
chproc.execSync(command, {
131+
execSync(command, {
129132
timeout: 1000 * 30
130133
})
131134

132-
const builtFile = fs.readFileSync('./out.mjs').toString()
135+
const builtFile = readFileSync('./out.mjs').toString()
133136
assert.include(builtFile, 'globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);')
134137
})
135138

136139
it('should contain the definitions when esm is inferred from format', () => {
137140
const command = 'node ./build-and-run.esm-relying-in-format.mjs'
138141
console.log(command)
139-
chproc.execSync(command, {
142+
execSync(command, {
140143
timeout: 1000 * 30
141144
})
142145

143-
const builtFile = fs.readFileSync('./out.mjs').toString()
146+
const builtFile = readFileSync('./out.mjs').toString()
144147
assert.include(builtFile, 'globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);')
145148
})
146149

147150
it('should contain the definitions when format is inferred from out extension', () => {
148151
const command = 'node ./build-and-run.esm-relying-in-out-extension.mjs'
149152
console.log(command)
150-
chproc.execSync(command, {
153+
execSync(command, {
151154
timeout: 1000 * 30
152155
})
153156

154-
const builtFile = fs.readFileSync('./basic-test.mjs').toString()
157+
const builtFile = readFileSync('./basic-test.mjs').toString()
155158
assert.include(builtFile, 'globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);')
156159
})
157160

158161
it('should not contain the definitions when no esm is specified', () => {
159162
const command = 'node ./build.js'
160163
console.log(command)
161-
chproc.execSync(command, {
164+
execSync(command, {
162165
timeout: 1000 * 30
163166
})
164167

165-
const builtFile = fs.readFileSync('./out.js').toString()
168+
const builtFile = readFileSync('./out.js').toString()
166169
assert.notInclude(builtFile, 'globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);')
167170
})
168171

169172
it('should not crash when it is already patched using global', () => {
170173
const command = 'node ./build-and-run.esm-patched-global-banner.mjs'
171174
console.log(command)
172-
chproc.execSync(command, {
175+
execSync(command, {
173176
timeout: 1000 * 30
174177
})
175178
})
176179

177180
it('should not crash when it is already patched using const', () => {
178181
const command = 'node ./build-and-run.esm-patched-const-banner.mjs'
179182
console.log(command)
180-
chproc.execSync(command, {
183+
execSync(command, {
181184
timeout: 1000 * 30
182185
})
183186
})

0 commit comments

Comments
 (0)