Open qroac opened 5 years ago
To be able to use authenticated tests in the meantime, I extracted login and logout helpers to a new module. This works, at least almost.
Now my tests are quite unpredictable. Sometimes they succeed, sometimes they end up in errors caused by wrong authentication status and sometimes jist with "GraphQL Error: undefined", see example console outputs below.
To reproduce, take a fresh clone of the kits stable branch and
yarn add chai-subset
and add it to modules/testing/server-ts/integrationSetup.ts
import chaiSubset from 'chai-subset';
chai.use(chaiSubset);
I often use this in my tests, sorry
Now run the tests from the kits root directory with yarn tests
multiple times.
(OS: Manjaro Linux, node: 10.15.3)
server: FAIL ../../modules/custom-extensions/server-ts/__tests__/TestHelpers.Session.test.ts (7.55s)
server: ● CustomExtensions API works › Signing in as ordinary user works
server: TypeError: Cannot read property 'username' of null
server: 22 | await login('user', 'user1234');
server: 23 | const result = await apollo.query({ query: CURRENT_USER_QUERY });
server: > 24 | expect(result.data.currentUser.username).to.equal('user');
server: | ^
server: 25 | });
server: 26 |
server: 27 | test('Signing out as ordinary user works', async () => {
server: at Object.username (modules/custom-extensions/server-ts/__tests__/TestHelpers.Session.test.ts:24:36)
erver: FAIL ../../modules/balance/server-ts/__tests__/Balance.test.ts (9.863s)
server: ● Balance API works › Authenticated as user › Update full balance record works
server: GraphQL error: undefined
server: at new ApolloError (node_modules/apollo-client/bundle.umd.js:92:26)
server: at Object.next (node_modules/apollo-client/bundle.umd.js:1290:31)
server: at notifySubscription (node_modules/zen-observable/lib/Observable.js:130:18)
server: at onNotify (node_modules/zen-observable/lib/Observable.js:165:3)
server: at SubscriptionObserver.next (node_modules/zen-observable/lib/Observable.js:219:7)
server: at node_modules/apollo-client/bundle.umd.js:1095:36
server: at Set.forEach (<anonymous>)
server: at Object.next (node_modules/apollo-client/bundle.umd.js:1094:21)
server: at notifySubscription (node_modules/zen-observable/lib/Observable.js:130:18)
server: at onNotify (node_modules/zen-observable/lib/Observable.js:165:3)
server: at SubscriptionObserver.next (node_modules/zen-observable/lib/Observable.js:219:7)
server: at node_modules/apollo-link-batch/src/batching.ts:165:42
server: at Array.forEach (<anonymous>)
server: at node_modules/apollo-link-batch/src/batching.ts:165:26
server: at Array.forEach (<anonymous>)
server: at Object.next (node_modules/apollo-link-batch/src/batching.ts:163:17)
@theWeird Have you tried to run jest with --runInBand
option?
That worked for the errors of the second post. Thank you.
What do you think about moving login and logout test helpers to testing-server-ts?
I'd to a PR on that.
@theWeird I don't think they belong to testing
module, they rather belong to authentication module I think. It would be great if you could make them helpers and export from authentication module
Just added pull request #1129
The errors mentioned in the issues first post occour, when importing LOGIN and LOGOUT mutations directly from user-client-react and authentication-client-react but not when importing from the subpath inside these modules. So I imported the mutations directly from the files.
I also tried to embed a basic login mutation and the logout mutation with graphql-tag directly into the helpers file. Works, too.
If you prefer that, I'll update it.
Describe the bug I'm updating a current project to the latest stable of the kit, migrating from packages to modules structure.
Before, I just imported the user modules login/logout helper to use it for authenticated tests in other modules.
Now, importing them into anohter modules test file and trying to access the helpers, I get an error
Cannot find module './containers/Auth' from 'index.jsx'
.To Reproduce Steps to reproduce the behavior:
import { getApollo } from '@gqlapp/testing-server-ts'; import * as UserHelper from '@gqlapp/user-server-ts/testHelpers';
const INTROSPECTION_QUERY = gql
query introspectionQuery { __schema { types { name } } }
;describe('Testmodule API works', () => { let apollo: ApolloClient;
// Using the helpers causes the error beforeAll(async () => { apollo = getApollo(); await UserHelper.login('user', 'user1234'); }); afterAll(async () => { await UserHelper.logout(); });
it('Should send a query to the GraphQL back end', async () => { // Not using the helpers and just console.log the object causes the error console.log(UserHelper); const result = await apollo.query({ query: INTROSPECTION_QUERY }); expect(result.data).to.have.property('__schema'); }); });
server: FAIL ../../modules/testmodule/server-ts/tests/Testmodule.test.ts server: ● Test suite failed to run server: Cannot find module './containers/Auth' from 'index.jsx' server: However, Jest was able to find: server: 'containers/Auth.native.jsx' server: 'containers/Auth.web.jsx' server: You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node']. server: See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string server: 11 | import DataRootComponent from './containers/DataRootComponent'; server: 12 | server: > 13 | import { AuthRoute, IfLoggedIn, IfNotLoggedIn, withLoadedUser, withLogout } from './containers/Auth'; server: | ^ server: 14 | server: 15 | const ProfileName = withLoadedUser(({ currentUser }) => server: 16 | currentUser ? currentUser.fullName || currentUser.username : null server: at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:230:17) server: at Object. (modules/user/client-react/index.jsx:13:1)