Skip to content

Commit 2988be6

Browse files
committed
add tests for js/ts config files
1 parent b3d87c3 commit 2988be6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

packages/nextjs/src/config/webpack.ts

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

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

packages/nextjs/test/config.test.ts

Lines changed: 52 additions & 1 deletion
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';
@@ -357,4 +362,50 @@ describe('Sentry webpack plugin config', () => {
357362

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

0 commit comments

Comments
 (0)