Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login/logout Test helpers not working in other modules #1126

Open
qroac opened this issue Jul 17, 2019 · 5 comments
Open

login/logout Test helpers not working in other modules #1126

qroac opened this issue Jul 17, 2019 · 5 comments

Comments

@qroac
Copy link
Contributor

qroac commented Jul 17, 2019

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:

  1. Clone fresh project from current stable branch and execute yarn to install packages
  2. Choose stack to react and node via CLI
  3. Create a new module (e.g. 'yarn cli addmodule testmodule)
  4. Edit module/testmodule/server-ts/tests/Testmodule.test.ts to import the testHelpers call one of the helpers or just console.log them, result is the same.
import { expect } from 'chai';
import { ApolloClient } from 'apollo-client';
import gql from 'graphql-tag';

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<any>;

  // 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');
  });
});
  1. Run yarn tests

Expected behavior
If just passed to console.log, I'd expect to see an output of an object, having a login and a logout function.
If calling one of these functions I'd expect to have an authenticated or anonymous user for running further tests.

Actual console output

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.<anonymous> (modules/user/client-react/index.jsx:13:1)

Desktop (please complete the following information):

  • OS: Manjaro Linux, x64
  • Browser: Bash Terminal
  • Node Version: 10.15.3
@qroac
Copy link
Contributor Author

qroac commented Jul 22, 2019

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

  • add the attached modules to the module directory
    modules.zip
  • import and add the modules logging-server-ts and balance-server-ts to the ServerModules in packages/server/src/modules.ts
  • cd to packages/server, 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)

  1. It may succeed
  2. It may produce errors related to an unpredicted login status, e.g. user is authenticated but shouldn't be or vice versa (I am using login and logout in jests beforeEach and afterEach functions)
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)
  1. It may fail with the quite generic error: GraphQL error: undefined
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)

@larixer
Copy link
Member

larixer commented Jul 22, 2019

@Theweird Have you tried to run jest with --runInBand option?

@qroac
Copy link
Contributor Author

qroac commented Jul 22, 2019

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.

@larixer
Copy link
Member

larixer commented Jul 22, 2019

@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

@qroac
Copy link
Contributor Author

qroac commented Jul 22, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants