Skip to content

Commit 2a996ff

Browse files
authored
Run unit tests in series to remove race conditions (#11)
* test: run unit tests in series to remove race conditions * test: clear mocks between tests using beforeEach * test: slacken bundle and sourcemap filename tests
1 parent 7849816 commit 2a996ff

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

test/source-map-uploader-plugin.test.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Bugsnag from '@bugsnag/cli'
22
import { resolve } from 'path'
33
import { build } from 'vite'
4-
import { describe, expect, test, vi } from 'vitest'
4+
import { beforeEach, describe, expect, test, vi } from 'vitest'
55
import { BugsnagSourceMapUploaderPlugin } from '../src/source-map-uploader-plugin'
66
import cleanBuildDir from './lib/clean-build-dir'
77

@@ -16,6 +16,10 @@ vi.mock('@bugsnag/cli', () => ({
1616
}))
1717

1818
describe('BugsnagSourceMapUploaderPlugin', () => {
19+
beforeEach(() => {
20+
vi.mocked(Bugsnag.Upload.Js).mockClear()
21+
})
22+
1923
test('should return a valid plugin object', async () => {
2024
const plugin = BugsnagSourceMapUploaderPlugin({
2125
apiKey: 'test-api',
@@ -63,23 +67,22 @@ describe('BugsnagSourceMapUploaderPlugin', () => {
6367

6468
const sourcemapUpload = vi.mocked(Bugsnag.Upload.Js)
6569
const outputDir = resolve(fixturesPath, 'dist')
66-
const bundlePath = resolve(outputDir, 'assets/index-DTHX3LI9.js')
67-
const sourceMapPath = resolve(outputDir, 'assets/index-DTHX3LI9.js.map')
6870

6971
expect(mockLogger.info).toHaveBeenCalledWith('[BugsnagSourceMapUploaderPlugin] uploading sourcemaps using the bugsnag-cli')
7072
expect(mockLogger.info).toHaveBeenCalledWith('[BugsnagSourceMapUploaderPlugin] Sourcemaps uploaded successfully')
71-
expect(sourcemapUpload).toHaveBeenCalledExactlyOnceWith({
72-
apiKey: 'test-api',
73-
bundleUrl: 'https://bugsnag.com/assets/index-DTHX3LI9.js',
74-
bundle: bundlePath,
75-
projectRoot: fixturesPath,
76-
sourceMap: sourceMapPath,
77-
versionName: '1.0.0'
78-
},
79-
outputDir
80-
)
73+
expect(sourcemapUpload).toHaveBeenCalledOnce()
8174

82-
sourcemapUpload.mockClear()
75+
const [uploadOptions, targetDir] = sourcemapUpload.mock.calls[0]
76+
expect(uploadOptions).toBeDefined()
77+
expect(uploadOptions).toMatchObject({
78+
apiKey: 'test-api',
79+
projectRoot: fixturesPath,
80+
versionName: '1.0.0'
81+
})
82+
expect(uploadOptions!.bundleUrl).toMatch(/^https:\/\/bugsnag\.com\/assets\/index-[a-zA-Z0-9]+\.js$/)
83+
expect(uploadOptions!.bundle).toMatch(/\/assets\/index-[a-zA-Z0-9]+\.js$/)
84+
expect(uploadOptions!.sourceMap).toMatch(/\/assets\/index-[a-zA-Z0-9]+\.js\.map$/)
85+
expect(targetDir).toBe(outputDir)
8386
})
8487

8588
test('should use the relative filepath for bundleUrl if base is not provided in config', async () => {
@@ -108,23 +111,22 @@ describe('BugsnagSourceMapUploaderPlugin', () => {
108111

109112
const sourcemapUpload = vi.mocked(Bugsnag.Upload.Js)
110113
const outputDir = resolve(fixturePath, 'dist')
111-
const bundlePath = resolve(outputDir, 'assets/index-DTHX3LI9.js')
112-
const sourceMapPath = resolve(outputDir, 'assets/index-DTHX3LI9.js.map')
113114

114115
expect(mockLogger.info).toHaveBeenCalledWith('[BugsnagSourceMapUploaderPlugin] uploading sourcemaps using the bugsnag-cli')
115116
expect(mockLogger.info).toHaveBeenCalledWith('[BugsnagSourceMapUploaderPlugin] Sourcemaps uploaded successfully')
116-
expect(sourcemapUpload).toHaveBeenCalledExactlyOnceWith({
117-
apiKey: 'test-api',
118-
bundleUrl: '/assets/index-DTHX3LI9.js',
119-
bundle: bundlePath,
120-
projectRoot: fixturePath,
121-
sourceMap: sourceMapPath,
122-
versionName: version
123-
},
124-
outputDir
125-
)
126-
127-
sourcemapUpload.mockClear()
117+
expect(sourcemapUpload).toHaveBeenCalledOnce()
118+
119+
const [uploadOptions, targetDir] = sourcemapUpload.mock.calls[0]
120+
expect(uploadOptions).toBeDefined()
121+
expect(uploadOptions).toMatchObject({
122+
apiKey: 'test-api',
123+
projectRoot: fixturePath,
124+
versionName: version
125+
})
126+
expect(uploadOptions!.bundleUrl).toMatch(/^\/assets\/index-[a-zA-Z0-9]+\.js$/)
127+
expect(uploadOptions!.bundle).toMatch(/\/assets\/index-[a-zA-Z0-9]+\.js$/)
128+
expect(uploadOptions!.sourceMap).toMatch(/\/assets\/index-[a-zA-Z0-9]+\.js\.map$/)
129+
expect(targetDir).toBe(outputDir)
128130
})
129131

130132
test('logs an error if the upload fails', async () => {
@@ -159,7 +161,5 @@ describe('BugsnagSourceMapUploaderPlugin', () => {
159161
expect(mockLogger.info).toHaveBeenCalledWith('[BugsnagSourceMapUploaderPlugin] uploading sourcemaps using the bugsnag-cli')
160162
expect(mockLogger.error).toHaveBeenCalledWith('[BugsnagSourceMapUploaderPlugin] Error: Upload failed')
161163
expect(mockLogger.info).not.toHaveBeenCalledWith('[BugsnagSourceMapUploaderPlugin] Sourcemaps uploaded successfully')
162-
163-
sourcemapUpload.mockClear()
164164
})
165165
})

vitest.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
// Run tests in sequence to avoid race conditions with shared fixture directories
6+
fileParallelism: false
7+
}
8+
})

0 commit comments

Comments
 (0)