|
| 1 | +import { describe, jest, test, afterEach } from '@jest/globals'; |
| 2 | + |
| 3 | +// setup mocks |
| 4 | +jest.unstable_mockModule('node:url', () => ({ |
| 5 | + pathToFileURL: (path) => ({ href: path }), |
| 6 | +})); |
| 7 | +class UserSolutionSyntaxErrorMock extends Error { |
| 8 | + constructor() { |
| 9 | + super(); |
| 10 | + this.name = 'UserSolutionSyntaxErrorMock'; |
| 11 | + } |
| 12 | +} |
| 13 | +class UserSolutionFileNotFoundErrorMock extends Error { |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + this.name = 'UserSolutionFileNotFoundErrorMock'; |
| 17 | + } |
| 18 | +} |
| 19 | +jest.unstable_mockModule('src/errors/solutionWorkerErrors.js', () => ({ |
| 20 | + UserSolutionFileNotFoundError: UserSolutionFileNotFoundErrorMock, |
| 21 | + UserSolutionSyntaxError: UserSolutionSyntaxErrorMock, |
| 22 | +})); |
| 23 | +// import after mocks set up. |
| 24 | + |
| 25 | +describe('importUserSolutionModule', () => { |
| 26 | + afterEach(() => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + jest.resetModules(); |
| 29 | + }); |
| 30 | + |
| 31 | + test('returns module', async () => { |
| 32 | + // mock an module we know exists |
| 33 | + const modulePath = 'src/main.js'; |
| 34 | + const module = { levelOne: () => {} }; |
| 35 | + jest.unstable_mockModule(modulePath, () => module); |
| 36 | + |
| 37 | + const { importUserSolutionModule } = await import( |
| 38 | + '../../src/solutions/importUserSolutionModule.js' |
| 39 | + ); |
| 40 | + const result = await importUserSolutionModule(modulePath); |
| 41 | + expect(result).toMatchObject(module); |
| 42 | + }); |
| 43 | + |
| 44 | + test('throws UserSolutionFileNotFoundError if file not found', async () => { |
| 45 | + // mock an module we know exists |
| 46 | + const modulePath = 'src/main.js'; |
| 47 | + const error = new Error(); |
| 48 | + error.code = 'ERR_MODULE_NOT_FOUND'; |
| 49 | + jest.unstable_mockModule(modulePath, () => { |
| 50 | + throw error; |
| 51 | + }); |
| 52 | + |
| 53 | + const { importUserSolutionModule } = await import( |
| 54 | + '../../src/solutions/importUserSolutionModule.js' |
| 55 | + ); |
| 56 | + |
| 57 | + await expect(async () => importUserSolutionModule(modulePath)).rejects.toThrow( |
| 58 | + UserSolutionFileNotFoundErrorMock |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test('throws UserSolutionSyntaxError if file has syntax error', async () => { |
| 63 | + // mock an module we know exists |
| 64 | + const modulePath = 'src/main.js'; |
| 65 | + jest.unstable_mockModule(modulePath, () => { |
| 66 | + throw new SyntaxError(); |
| 67 | + }); |
| 68 | + |
| 69 | + const { importUserSolutionModule } = await import( |
| 70 | + '../../src/solutions/importUserSolutionModule.js' |
| 71 | + ); |
| 72 | + |
| 73 | + await expect(async () => importUserSolutionModule(modulePath)).rejects.toThrow( |
| 74 | + UserSolutionSyntaxErrorMock |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + test('throws on unknown error', async () => { |
| 79 | + // mock an module we know exists |
| 80 | + const modulePath = 'src/main.js'; |
| 81 | + jest.unstable_mockModule(modulePath, () => { |
| 82 | + throw new RangeError(); |
| 83 | + }); |
| 84 | + |
| 85 | + const { importUserSolutionModule } = await import( |
| 86 | + '../../src/solutions/importUserSolutionModule.js' |
| 87 | + ); |
| 88 | + |
| 89 | + await expect(async () => importUserSolutionModule(modulePath)).rejects.toThrow( |
| 90 | + RangeError |
| 91 | + ); |
| 92 | + }); |
| 93 | +}); |
0 commit comments