Skip to content

Commit 8e910b6

Browse files
committed
add tests for js/ts config files
1 parent d47ee55 commit 8e910b6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

packages/nextjs/src/config/webpack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async function addSentryToEntryProperty(
153153
* @param platform Either "server" or "client", so that we know which file to look for
154154
* @returns The name of the relevant file. If no file is found, this method throws an error.
155155
*/
156-
function getUserConfigFile(projectDir: string, platform: 'server' | 'client'): string {
156+
export function getUserConfigFile(projectDir: string, platform: 'server' | 'client'): string {
157157
let configFile;
158158

159159
const possibilities = [`sentry.${platform}.config.ts`, `sentry.${platform}.config.js`];

packages/nextjs/test/config.test.ts

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import * as fs from 'fs';
2+
import * as os from 'os';
3+
import * as path from 'path';
4+
import * as rimraf from 'rimraf';
5+
16
import { withSentryConfig } from '../src/config';
27
import {
38
BuildContext,
@@ -7,7 +12,7 @@ import {
712
SentryWebpackPluginOptions,
813
WebpackConfigObject,
914
} from '../src/config/types';
10-
import { constructWebpackConfigFunction, SentryWebpackPlugin } from '../src/config/webpack';
15+
import { constructWebpackConfigFunction, getUserConfigFile, SentryWebpackPlugin } from '../src/config/webpack';
1116

1217
const SERVER_SDK_CONFIG_FILE = 'sentry.server.config.js';
1318
const CLIENT_SDK_CONFIG_FILE = 'sentry.client.config.js';
@@ -360,4 +365,50 @@ describe('Sentry webpack plugin config', () => {
360365

361366
expect(finalWebpackConfig?.devtool).not.toEqual('source-map');
362367
});
368+
369+
describe('getUserConfigFile', () => {
370+
let tempDir: string;
371+
372+
beforeAll(() => {
373+
exitsSync.mockImplementation(realExistsSync);
374+
});
375+
376+
beforeEach(() => {
377+
const tempDirPathPrefix = path.join(os.tmpdir(), 'sentry-nextjs-test-');
378+
tempDir = fs.mkdtempSync(tempDirPathPrefix);
379+
});
380+
381+
afterEach(() => {
382+
rimraf.sync(tempDir);
383+
});
384+
385+
afterAll(() => {
386+
exitsSync.mockImplementation(mockExistsSync);
387+
});
388+
389+
it('successfully finds js files', () => {
390+
fs.writeFileSync(path.resolve(tempDir, 'sentry.server.config.js'), 'Dogs are great!');
391+
fs.writeFileSync(path.resolve(tempDir, 'sentry.client.config.js'), 'Squirrel!');
392+
393+
expect(getUserConfigFile(tempDir, 'server')).toEqual('sentry.server.config.js');
394+
expect(getUserConfigFile(tempDir, 'client')).toEqual('sentry.client.config.js');
395+
});
396+
397+
it('successfully finds ts files', () => {
398+
fs.writeFileSync(path.resolve(tempDir, 'sentry.server.config.ts'), 'Sit. Stay. Lie Down.');
399+
fs.writeFileSync(path.resolve(tempDir, 'sentry.client.config.ts'), 'Good dog!');
400+
401+
expect(getUserConfigFile(tempDir, 'server')).toEqual('sentry.server.config.ts');
402+
expect(getUserConfigFile(tempDir, 'client')).toEqual('sentry.client.config.ts');
403+
});
404+
405+
it('errors when files are missing', () => {
406+
expect(() => getUserConfigFile(tempDir, 'server')).toThrowError(
407+
`Cannot find 'sentry.server.config.ts' or 'sentry.server.config.js' in '${tempDir}'`,
408+
);
409+
expect(() => getUserConfigFile(tempDir, 'client')).toThrowError(
410+
`Cannot find 'sentry.client.config.ts' or 'sentry.client.config.js' in '${tempDir}'`,
411+
);
412+
});
413+
});
363414
});

0 commit comments

Comments
 (0)