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

@loopback/testlab requires mocha, conflicts with @types/jest if @types/mocha is brought in #2452

Closed
vazexqi opened this issue Feb 22, 2019 · 4 comments · Fixed by #3013
Closed
Assignees
Labels
feature Internal Tooling Issues related to our tooling and monorepo infrastructore

Comments

@vazexqi
Copy link

vazexqi commented Feb 22, 2019

Description / Steps to reproduce / Feature proposal

I would like to use jest instead of mocha as my testing framework. At the same time, I would also like to use @loopback/testlab for some of its convenience functions.

However, the current version @loopback/testlab requires mocha's type definition to be available. Specifically, this is in https://github.com/strongloop/loopback-next/blob/master/packages/testlab/src/skip-travis.ts

Current Behavior

If I don't bring in @types/mocha, I get the following error:

$ tsc --project .
../../node_modules/@loopback/testlab/dist/skip-travis.d.ts(1,23): error TS2688: Cannot find type definition file for 'mocha'.
../../node_modules/@loopback/testlab/dist/skip-travis.d.ts(8,82): error TS2503: Cannot find namespace 'Mocha'.
../../node_modules/@loopback/testlab/dist/skip-travis.d.ts(8,116): error TS2304: Cannot find name 'MochaDone'.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

If I bring in @types/mocha, I get the following error:

$ tsc --project .
../../node_modules/@types/mocha/index.d.ts(2680,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'beforeEach' must be of type 'Lifecycle', but here has type 'HookFunction'.
../../node_modules/@types/mocha/index.d.ts(2698,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'afterEach' must be of type 'Lifecycle', but here has type 'HookFunction'.
../../node_modules/@types/mocha/index.d.ts(2714,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'Describe', but here has type 'SuiteFunction'.
../../node_modules/@types/mocha/index.d.ts(2735,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'xdescribe' must be of type 'Describe', but here has type 'PendingSuiteFunction'.
../../node_modules/@types/mocha/index.d.ts(2749,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'it' must be of type 'It', but here has type 'TestFunction'.
../../node_modules/@types/mocha/index.d.ts(2763,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'test' must be of type 'It', but here has type 'TestFunction'.
../../node_modules/@types/mocha/index.d.ts(2770,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'xit' must be of type 'It', but here has type 'PendingTestFunction'.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Expected Behavior

I would like to be able to use @loopback/testlab without it referencing mocha.

Is this working-as-design (WAD)? I am not sure how tightly coupled @loopback/testlab wants to be to mocha since it's not declared explicitly in https://github.com/strongloop/loopback-next/blob/master/packages/testlab/package.json. It's part of a transitive dependency on @loopback/build.

@bajtos bajtos self-assigned this May 24, 2019
@bajtos bajtos added feature Internal Tooling Issues related to our tooling and monorepo infrastructore labels May 24, 2019
@bajtos
Copy link
Member

bajtos commented May 24, 2019

Hi @vazexqi, thank you for raising this issue. Is the problem still relevant? Did you manage to find any workaround?

Is this working-as-design (WAD)? I am not sure how tightly coupled @loopback/testlab wants to be to mocha since it's not declared explicitly in https://github.com/strongloop/loopback-next/blob/master/packages/testlab/package.json. It's part of a transitive dependency on @loopback/build.

That's a good question! To be honest, I haven't considered this aspect before.

I think ideally, I'd like the helper to support different test runners. On Mocha, we want to call it.skip, in Jest test.skip.

I think we should rework the helper into a factory accepting it/test/etc. as provided by the test framework, and returning the helper function calling the provided test API under the hood.

import {createSkippedOnTravis} from '@loopback/testlab';

// Mocha
const itSkippedOnTravis = createSkippedOnTravis(it);

// Jest
const itSkippedOnTravis = createSkippedOnTravis(test);

What do you think?

On an unrelated note, I am experimenting with migrating loopback-next from Mocha to Jest (with ts-jest plugin to compile TS files on the fly). One of the problems I have discovered is that Reflection does not seem to work in such setup, have you encountered this problem yourself too?

@bajtos
Copy link
Member

bajtos commented May 31, 2019

I opened #3013 to fix the problem.

@samarpanB
Copy link
Contributor

@bajtos do we have any documentation on how to use jest with testlab in LoopBack? I am kind of intrigued to try this suite.

@bajtos
Copy link
Member

bajtos commented Jul 12, 2019

@samarpanB testlab should be agnostic to the test runner used. Just replace mocha (and lb-mocha) with jest. You can find Jest configuration for loopback-next that I am experimenting with here: https://github.com/strongloop/loopback-next/tree/test/jest, see jest.config in particular.

Please note that @loopback/boot is not compatible with ts-jest (and ts-mocha too), it is always looking for transpiled .js files and cannot load .ts files yet. The workaround is to configure every booter to allow both .js and .ts files, e.g. extensions: ['.controller.js', '.controller.ts'].

I'd like to improve @loopback/boot to support .ts files too, but haven't had bandwidth for that yet. Would you like to help?

BTW I am usually ignoring comments in closed issues and pull request. Please open a new issue if you would like to continue in this dicussion.

@loopbackio loopbackio locked as resolved and limited conversation to collaborators Jul 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Internal Tooling Issues related to our tooling and monorepo infrastructore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants