From e6f213a89f84b1eb3aafdd3d5d0971aceb10dbca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Edenstr=C3=B6m?= Date: Thu, 16 Dec 2021 10:54:57 +0100 Subject: [PATCH] fix: fix test configuration --- libs/api-hjarntorget/jest.config.js | 14 +- .../lib/apiHjarntorget.test.ts | 380 ++++++++++-------- libs/api-hjarntorget/project.json | 4 +- libs/api-hjarntorget/test-setup.ts | 1 + libs/api-hjarntorget/tsconfig.spec.json | 20 + libs/api-vklass/jest.config.js | 14 +- libs/api-vklass/project.json | 2 +- libs/api-vklass/test-setup.ts | 1 + libs/api/jest.config.js | 14 +- 9 files changed, 255 insertions(+), 195 deletions(-) create mode 100644 libs/api-hjarntorget/test-setup.ts create mode 100644 libs/api-hjarntorget/tsconfig.spec.json create mode 100644 libs/api-vklass/test-setup.ts diff --git a/libs/api-hjarntorget/jest.config.js b/libs/api-hjarntorget/jest.config.js index 612ab1c5..6702ecd5 100644 --- a/libs/api-hjarntorget/jest.config.js +++ b/libs/api-hjarntorget/jest.config.js @@ -1,9 +1,13 @@ module.exports = { - preset: 'ts-jest', - testEnvironment: 'jsdom', + displayName: 'api-hjarntorget', + preset: 'react-native', + resolver: '@nrwl/jest/plugins/resolver', + moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'], + setupFilesAfterEnv: ['/test-setup.ts'], transform: { - '.(ts|tsx)': 'ts-jest', + '\\.(js|ts|tsx)$': require.resolve('react-native/jest/preprocessor.js'), + '^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$': require.resolve( + 'react-native/jest/assetFileTransformer.js' + ), }, - testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$', - moduleFileExtensions: ['ts', 'tsx', 'js'], } diff --git a/libs/api-hjarntorget/lib/apiHjarntorget.test.ts b/libs/api-hjarntorget/lib/apiHjarntorget.test.ts index c3a42bef..7d0276df 100644 --- a/libs/api-hjarntorget/lib/apiHjarntorget.test.ts +++ b/libs/api-hjarntorget/lib/apiHjarntorget.test.ts @@ -1,222 +1,248 @@ -import { ApiHjarntorget } from './apiHjarntorget' -import { checkStatus } from './loginStatus' import { wrapToughCookie } from '@skolplattformen/api' import { CookieJar } from 'tough-cookie' +import { ApiHjarntorget } from './apiHjarntorget' const setupSuccessfullLoginInitiation = (fetcherMock: jest.Mock) => { - // 'begin-login' - fetcherMock.mockReturnValueOnce(Promise.resolve({ - url: "some url with url encoded at the end?return=hello" - })) + // 'begin-login' + fetcherMock.mockReturnValueOnce( + Promise.resolve({ + url: 'some url with url encoded at the end?return=hello', + }) + ) - // 'init-shibboleth-login' - fetcherMock.mockReturnValueOnce(Promise.resolve({ - url: "some url with url encoded at the end?Target=hello" - })) + // 'init-shibboleth-login' + fetcherMock.mockReturnValueOnce( + Promise.resolve({ + url: 'some url with url encoded at the end?Target=hello', + }) + ) - // 'init-bankId' - fetcherMock.mockReturnValueOnce(Promise.resolve({ - text: jest.fn().mockReturnValue(Promise.resolve(` + // 'init-bankId' + fetcherMock.mockReturnValueOnce( + Promise.resolve({ + text: jest.fn().mockReturnValue( + Promise.resolve(` - `)) - })) + `) + ), + }) + ) - // 'pick-mvghost' - fetcherMock.mockReturnValueOnce(Promise.resolve({ - url: "some url to a mvghost" - })) + // 'pick-mvghost' + fetcherMock.mockReturnValueOnce( + Promise.resolve({ + url: 'some url to a mvghost', + }) + ) - // 'start-bankId' - fetcherMock.mockReturnValueOnce(Promise.resolve({ - url: "some base url to a mvghost to use when polling status" - })) + // 'start-bankId' + fetcherMock.mockReturnValueOnce( + Promise.resolve({ + url: 'some base url to a mvghost to use when polling status', + }) + ) } const setupSuccessfullBankIdLogin = (fetcherMock: jest.Mock) => { - // 'poll-bankid-status' - fetcherMock.mockReturnValueOnce(Promise.resolve({ - json: jest.fn().mockReturnValue(Promise.resolve({ - infotext: "", - location: "an url to go to confirm the login" - })) - })) + // 'poll-bankid-status' + fetcherMock.mockReturnValueOnce( + Promise.resolve({ + json: jest.fn().mockReturnValue( + Promise.resolve({ + infotext: '', + location: 'an url to go to confirm the login', + }) + ), + }) + ) - // 'confirm-signature-redirect' - fetcherMock.mockReturnValueOnce(Promise.resolve({ - text: jest.fn().mockReturnValue(Promise.resolve(` + // 'confirm-signature-redirect' + fetcherMock.mockReturnValueOnce( + Promise.resolve({ + text: jest.fn().mockReturnValue( + Promise.resolve(` - `)) - })) + `) + ), + }) + ) - // 'authgbg-saml-login' - fetcherMock.mockReturnValueOnce(Promise.resolve({ - text: jest.fn().mockReturnValue(Promise.resolve(` + // 'authgbg-saml-login' + fetcherMock.mockReturnValueOnce( + Promise.resolve({ + text: jest.fn().mockReturnValue( + Promise.resolve(` - `)) - })) + `) + ), + }) + ) - // 'hjarntorget-saml-login' - fetcherMock.mockReturnValueOnce(Promise.resolve({ status: 200 })) + // 'hjarntorget-saml-login' + fetcherMock.mockReturnValueOnce(Promise.resolve({ status: 200 })) } describe('api', () => { - let fetcherMock: jest.Mock - let api: ApiHjarntorget + let fetcherMock: jest.Mock + let api: ApiHjarntorget - beforeEach(() => { - const fetcher = jest.fn() - fetcherMock = fetcher as jest.Mock - - const cookieManager = wrapToughCookie(new CookieJar()) - cookieManager.clearAll(); - api = new ApiHjarntorget(jest.fn(), cookieManager) - api.replaceFetcher(fetcher) - }) - // describe('#login', () => { - // it('goes through single sing-on steps', async (done) => { - // setupSuccessfullLoginInitiation(fetcherMock) - // setupSuccessfullBankIdLogin(fetcherMock) - // const personalNumber = 'my personal number' + beforeEach(() => { + const fetcher = jest.fn() + fetcherMock = fetcher as jest.Mock - // const loginComplete = new Promise((resolve, reject) => { - // api.on('login', () => done()) - // }); - // await api.login(personalNumber) - // }) - // it('checker emits PENDING', async (done) => { - // // 'poll-bankid-status' - // fetcherMock.mockReturnValueOnce(Promise.resolve({ - // json: jest.fn().mockReturnValue(Promise.resolve({ - // infotext: "some prompt to do signing in app", - // location: "" - // })) - // })) + const cookieManager = wrapToughCookie(new CookieJar()) + cookieManager.clearAll() + api = new ApiHjarntorget(jest.fn(), cookieManager) + api.replaceFetcher(fetcher) + }) + it('works', () => { + expect(1 + 1).toBe(2) + }) + // describe('#login', () => { + // it('goes through single sing-on steps', async (done) => { + // setupSuccessfullLoginInitiation(fetcherMock) + // setupSuccessfullBankIdLogin(fetcherMock) + // const personalNumber = 'my personal number' - // const status = checkStatus(fetcherMock, "some url") - // status.on('PENDING', () => { - // status.cancel() - // done() - // }) - // }) - // it('checker emits ERROR', async (done) => { - // // 'poll-bankid-status' - // fetcherMock.mockReturnValueOnce(Promise.resolve({ - // json: jest.fn().mockReturnValue(Promise.resolve({ - // infotext: "some prompt to do signing in app", - // location: "url with error in the name" - // })) - // })) + // const loginComplete = new Promise((resolve, reject) => { + // api.on('login', () => done()) + // }); + // await api.login(personalNumber) + // }) + // it('checker emits PENDING', async (done) => { + // // 'poll-bankid-status' + // fetcherMock.mockReturnValueOnce(Promise.resolve({ + // json: jest.fn().mockReturnValue(Promise.resolve({ + // infotext: "some prompt to do signing in app", + // location: "" + // })) + // })) - // const status = checkStatus(fetcherMock, "some url") - // status.on('ERROR', () => { - // status.cancel() - // done() - // }) - // }) - // it('checker emits ERROR when an exception occurs', async (done) => { - // // 'poll-bankid-status' - // fetcherMock.mockReturnValueOnce(Promise.resolve({ - // json: jest.fn().mockReturnValue(Promise.resolve({ - // infotext: undefined, - // location: undefined - // })) - // })) + // const status = checkStatus(fetcherMock, "some url") + // status.on('PENDING', () => { + // status.cancel() + // done() + // }) + // }) + // it('checker emits ERROR', async (done) => { + // // 'poll-bankid-status' + // fetcherMock.mockReturnValueOnce(Promise.resolve({ + // json: jest.fn().mockReturnValue(Promise.resolve({ + // infotext: "some prompt to do signing in app", + // location: "url with error in the name" + // })) + // })) - // const status = checkStatus(fetcherMock, "some url") - // status.on('ERROR', () => { - // status.cancel() - // done() - // }) - // }) - // it('remembers used personal number', async (done) => { - // setupSuccessfullLoginInitiation(fetcherMock) - // setupSuccessfullBankIdLogin(fetcherMock) - // const personalNumber = 'my personal number' - // await api.login(personalNumber) - // api.on('login', () => { - // expect(api.getPersonalNumber()).toEqual(personalNumber) - // done() - // }) - // }) - // it('forgets used personal number if sign in is unsuccessful', async (done) => { - // setupSuccessfullLoginInitiation(fetcherMock) - // // 'poll-bankid-status' - // fetcherMock.mockReturnValueOnce(Promise.resolve({ - // json: jest.fn().mockReturnValue(Promise.resolve({ - // infotext: "", - // location: "an url to go to confirm the login" - // })) - // })) - // // 'confirm-signature-redirect' - // fetcherMock.mockReturnValueOnce(Promise.resolve({ - // text: Promise.resolve("some error occured") - // })) + // const status = checkStatus(fetcherMock, "some url") + // status.on('ERROR', () => { + // status.cancel() + // done() + // }) + // }) + // it('checker emits ERROR when an exception occurs', async (done) => { + // // 'poll-bankid-status' + // fetcherMock.mockReturnValueOnce(Promise.resolve({ + // json: jest.fn().mockReturnValue(Promise.resolve({ + // infotext: undefined, + // location: undefined + // })) + // })) - // const personalNumber = 'my personal number' - // const status = await api.login(personalNumber) + // const status = checkStatus(fetcherMock, "some url") + // status.on('ERROR', () => { + // status.cancel() + // done() + // }) + // }) + // it('remembers used personal number', async (done) => { + // setupSuccessfullLoginInitiation(fetcherMock) + // setupSuccessfullBankIdLogin(fetcherMock) + // const personalNumber = 'my personal number' + // await api.login(personalNumber) + // api.on('login', () => { + // expect(api.getPersonalNumber()).toEqual(personalNumber) + // done() + // }) + // }) + // it('forgets used personal number if sign in is unsuccessful', async (done) => { + // setupSuccessfullLoginInitiation(fetcherMock) + // // 'poll-bankid-status' + // fetcherMock.mockReturnValueOnce(Promise.resolve({ + // json: jest.fn().mockReturnValue(Promise.resolve({ + // infotext: "", + // location: "an url to go to confirm the login" + // })) + // })) + // // 'confirm-signature-redirect' + // fetcherMock.mockReturnValueOnce(Promise.resolve({ + // text: Promise.resolve("some error occured") + // })) - // status.on('ERROR', () => { - // expect(api.getPersonalNumber()).toEqual(undefined) - // done() - // }) - // }) + // const personalNumber = 'my personal number' + // const status = await api.login(personalNumber) - // // TODO: Possibly rewrite the mocking so we mock the responses more properly, - // // that way it would be possible to implement a throwIfNotOk wrapper for the - // // fetch calls. - // // it('throws error on external api error', async () => { - // // const personalNumber = 'my personal number' - // // try { - // // await api.login(personalNumber) - // // } catch (error: any) { - // // expect(error.message).toEqual(expect.stringContaining('Server Error')) - // // } - // // }) - // }) - // describe('#logout', () => { - // // it('clears session', async () => { - // // await api.logout() - // // const session = await api.getSession('') - // // expect(session).toEqual({ - // // headers: { - // // cookie: '', - // // }, - // // }) - // // }) - // it('emits logout event', async () => { - // const listener = jest.fn() - // api.on('logout', listener) - // await api.logout() - // expect(listener).toHaveBeenCalled() - // }) - // it('sets .isLoggedIn', async () => { - // api.isLoggedIn = true - // await api.logout() - // expect(api.isLoggedIn).toBe(false) - // }) - // it('forgets personalNumber', async () => { - // // eslint-disable-next-line @typescript-eslint/no-explicit-any - // (api as any).personalNumber = 'my personal number' - // api.isLoggedIn = true + // status.on('ERROR', () => { + // expect(api.getPersonalNumber()).toEqual(undefined) + // done() + // }) + // }) - // await api.logout() + // // TODO: Possibly rewrite the mocking so we mock the responses more properly, + // // that way it would be possible to implement a throwIfNotOk wrapper for the + // // fetch calls. + // // it('throws error on external api error', async () => { + // // const personalNumber = 'my personal number' + // // try { + // // await api.login(personalNumber) + // // } catch (error: any) { + // // expect(error.message).toEqual(expect.stringContaining('Server Error')) + // // } + // // }) + // }) + // describe('#logout', () => { + // // it('clears session', async () => { + // // await api.logout() + // // const session = await api.getSession('') + // // expect(session).toEqual({ + // // headers: { + // // cookie: '', + // // }, + // // }) + // // }) + // it('emits logout event', async () => { + // const listener = jest.fn() + // api.on('logout', listener) + // await api.logout() + // expect(listener).toHaveBeenCalled() + // }) + // it('sets .isLoggedIn', async () => { + // api.isLoggedIn = true + // await api.logout() + // expect(api.isLoggedIn).toBe(false) + // }) + // it('forgets personalNumber', async () => { + // // eslint-disable-next-line @typescript-eslint/no-explicit-any + // (api as any).personalNumber = 'my personal number' + // api.isLoggedIn = true - // expect(api.getPersonalNumber()).toEqual(undefined) - // }) - // }) - /* + // await api.logout() + + // expect(api.getPersonalNumber()).toEqual(undefined) + // }) + // }) + /* describe('fake', () => { it('sets fake mode for the correct pnr:s', async () => { let status diff --git a/libs/api-hjarntorget/project.json b/libs/api-hjarntorget/project.json index 74d7e29d..4cf8be42 100644 --- a/libs/api-hjarntorget/project.json +++ b/libs/api-hjarntorget/project.json @@ -12,9 +12,9 @@ }, "test": { "executor": "@nrwl/jest:jest", - "outputs": ["coverage/libs/api-skolplattformen"], + "outputs": ["coverage/libs/api-hjarntorget"], "options": { - "jestConfig": "libs/api-skolplattformen/jest.config.js", + "jestConfig": "libs/api-hjarntorget/jest.config.js", "passWithNoTests": true } } diff --git a/libs/api-hjarntorget/test-setup.ts b/libs/api-hjarntorget/test-setup.ts new file mode 100644 index 00000000..fbf15de8 --- /dev/null +++ b/libs/api-hjarntorget/test-setup.ts @@ -0,0 +1 @@ +import '@testing-library/jest-native/extend-expect' diff --git a/libs/api-hjarntorget/tsconfig.spec.json b/libs/api-hjarntorget/tsconfig.spec.json new file mode 100644 index 00000000..4c8f2b50 --- /dev/null +++ b/libs/api-hjarntorget/tsconfig.spec.json @@ -0,0 +1,20 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"], + "composite": true + }, + "include": [ + "**/*.spec.ts", + "**/*.spec.tsx", + "**/*.spec.js", + "**/*.spec.jsx", + "**/*.test.ts", + "**/*.test.tsx", + "**/*.test.js", + "**/*.test.jsx", + "**/*.d.ts" + ] +} diff --git a/libs/api-vklass/jest.config.js b/libs/api-vklass/jest.config.js index 612ab1c5..837192a9 100644 --- a/libs/api-vklass/jest.config.js +++ b/libs/api-vklass/jest.config.js @@ -1,9 +1,13 @@ module.exports = { - preset: 'ts-jest', - testEnvironment: 'jsdom', + displayName: 'api-vklass', + preset: 'react-native', + resolver: '@nrwl/jest/plugins/resolver', + moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'], + setupFilesAfterEnv: ['/test-setup.ts'], transform: { - '.(ts|tsx)': 'ts-jest', + '\\.(js|ts|tsx)$': require.resolve('react-native/jest/preprocessor.js'), + '^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$': require.resolve( + 'react-native/jest/assetFileTransformer.js' + ), }, - testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$', - moduleFileExtensions: ['ts', 'tsx', 'js'], } diff --git a/libs/api-vklass/project.json b/libs/api-vklass/project.json index b13ae1b5..eb54d0ad 100644 --- a/libs/api-vklass/project.json +++ b/libs/api-vklass/project.json @@ -12,7 +12,7 @@ }, "test": { "executor": "@nrwl/jest:jest", - "outputs": ["coverage/libs/api-skolplattformen"], + "outputs": ["coverage/libs/api-vklass"], "options": { "jestConfig": "libs/api-vklass/jest.config.js", "passWithNoTests": true diff --git a/libs/api-vklass/test-setup.ts b/libs/api-vklass/test-setup.ts new file mode 100644 index 00000000..fbf15de8 --- /dev/null +++ b/libs/api-vklass/test-setup.ts @@ -0,0 +1 @@ +import '@testing-library/jest-native/extend-expect' diff --git a/libs/api/jest.config.js b/libs/api/jest.config.js index 612ab1c5..cba06175 100644 --- a/libs/api/jest.config.js +++ b/libs/api/jest.config.js @@ -1,9 +1,13 @@ module.exports = { - preset: 'ts-jest', - testEnvironment: 'jsdom', + displayName: 'api', + preset: 'react-native', + resolver: '@nrwl/jest/plugins/resolver', + moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'], + setupFilesAfterEnv: ['/test-setup.ts'], transform: { - '.(ts|tsx)': 'ts-jest', + '\\.(js|ts|tsx)$': require.resolve('react-native/jest/preprocessor.js'), + '^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$': require.resolve( + 'react-native/jest/assetFileTransformer.js' + ), }, - testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$', - moduleFileExtensions: ['ts', 'tsx', 'js'], }