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

Can't import mudder 1.1.1 with jest #19

Closed
tiagocpeixoto opened this issue Feb 3, 2022 · 14 comments
Closed

Can't import mudder 1.1.1 with jest #19

tiagocpeixoto opened this issue Feb 3, 2022 · 14 comments

Comments

@tiagocpeixoto
Copy link
Contributor

tiagocpeixoto commented Feb 3, 2022

When importing version 1.1.1 using Typescript, I got the following error:

Cannot read properties of undefined (reading 'mudder')
TypeError: Cannot read properties of undefined (reading 'mudder')

I tried to import using these statements, without success:

import * as mudder from "mudder"; // used until version 1.1.0
import mudder = require("mudder"); 
const mudder = require("mudder");

The error only occurs with version 1.1.1. The previous version was ok.

Env:
Node version 16
Typescript version 4.5.5
Webpack version 5.68
Jest version 27.4.7

Test code:

import * as mudder from "mudder";
// import mudder = require("mudder");
// const mudder = require("mudder");

describe("tests", function () {
  it("test generate string", function () {
    expect(mudder.base62.mudder(1)).toBeTruthy();
  });
});
@fasiha
Copy link
Owner

fasiha commented Feb 3, 2022

Thanks for the note! Hmm, it seems to work when I try the following very basic steps: first,

mkdir newtest
npm init -y
npm i mudder typescript @types/mudder
npx tsc --init

Then I create the following in index.ts:

import * as mudder from 'mudder';
console.log(mudder.base62.mudder(1))
console.log(mudder.base62.mudder(3))

before finally running:

npx tsc -p .
node index.js

I get the following:

$ mudderts node index.js
[ 'U' ]
[ 'F', 'U', 'k' ]

Let me know if the above works? Because, the above works if I install npm i --exact --save-exact [email protected] to install 1.1.0. If the above is also failing for you, we might have to dig into what #18 changed in your workflow.

@tiagocpeixoto
Copy link
Contributor Author

Ok, thanks. I will check my workflow.
Just a note: as a breaking change, shouldn't version 1.1.1 be 2.0.0?

@tiagocpeixoto
Copy link
Contributor Author

tiagocpeixoto commented Feb 3, 2022

After checking my workflow and using your example as a starting point, I found that the problem occurs when testing with Jest (don't know why yet).

npm i jest ts-jest @types/jest

add the file jest.config.js

const { defaults: tsjPreset } = require("ts-jest/presets");

module.exports = {
  transform: {
    ...tsjPreset.transform,
  },
};

and the file test index.test.ts

import * as mudder from "mudder";
// import mudder = require("mudder");
// const mudder = require("mudder");

describe("tests", function () {
  it("test generate string", function () {
    expect(mudder.base62.mudder(1)).toBeTruthy();
  });
});

and then type

npx jest

The result:

TypeError: Cannot read properties of undefined (reading 'mudder')

       5 | describe("tests", function () {
       6 |   it("test generate string", function () {
    >  7 |     expect(mudder.base62.mudder(1)).toBeTruthy();
         |                          ^
       8 |     // expect("test").toBeTruthy();
       9 |   });
      10 | });

@tiagocpeixoto tiagocpeixoto changed the title Can't import mudder with typescript Can't import mudder with typescript/jest Feb 3, 2022
@fasiha
Copy link
Owner

fasiha commented Feb 4, 2022

Hmm, when I follow your instructions, I get

npx jest
 PASS  ./index.test.js
 PASS  ./index.test.ts (9.972 s)

Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        12.005 s
Ran all test suites.

Here's an orphan branch with the state of my directory right now: https://github.com/fasiha/mudderjs/tree/issue-19 Can you compare your state to mine and see where the diff is? Are we getting different versions of typescript/jest?

Just a note: as a breaking change, shouldn't version 1.1.1 be 2.0.0?

This is definitely the case but we all expected #18 to be a user-invisible change. If it turns out this is not an issue with your setup but is a broader problem, I'll definitely cut a 2.0 release.

Thanks for bearing with me while we figure this out!

@tiagocpeixoto
Copy link
Contributor Author

The issue-19 brunch works, indeed.

BUT when changing the mudder version to 1.1.1 (in package.json), the aforementioned error occurs.

Could you check it with mudder version 1.1.1?

@tiagocpeixoto
Copy link
Contributor Author

Any news about the tests?

@fasiha
Copy link
Owner

fasiha commented Feb 12, 2022

Thanks for the updated info and the poke @tiagocpeixoto! Sorry for the delay, I now see I was misled last week, and you're right, with the latest mudder, jest indeed fails.

@vitaliytv, sorry to bother you with this, I'm curious if you knew what might be causing this issue with jest? Before #18 (mudder 1.1.0) seems to work fine, but with #18 the following happens—first, the setup:

git clone [email protected]:fasiha/mudderjs.git issue-19
cd issue-19
git checkout issue-19
npm i
npx jest --clearCache
npx jest

This produces the following error:

    TypeError: Cannot read properties of undefined (reading 'mudder')

      5 | describe('tests', function() {
      6 |   it('test generate string', function() {
    > 7 |     expect(mudder.base62.mudder(1)).toBeTruthy();
        |                          ^
      8 |   });
      9 | });

I'm sure it's some weird Jest issue because I can run npx ts-node index.ts which imports mudder exactly the same way as index.test.ts and it works fine.

Potentially relevant links that I consulted that didn't help:

@vitaliytv
Copy link
Contributor

thank you
i reproduced this error with jest,
this is not related to typescript, because with javasript also fails:

import { jest } from '@jest/globals'
import * as mudder from 'mudder'

test('Empty options', () => {
  expect(mudder.base62.mudder(1)).toBeTruthy()
})

i recommend use something other for example test director:

import { deepEqual } from 'assert'
import TestDirector from 'test-director'
import * as mudder from 'mudder'

const tests = new TestDirector()

tests.add('test director', async () => {
  deepEqual(mudder.base62.mudder(1), ['U'])
})

tests.run()

npx coverage-node test/index.js

@tiagocpeixoto tiagocpeixoto changed the title Can't import mudder with typescript/jest Can't import mudder 1.1.1 with jest Mar 13, 2022
@tiagocpeixoto
Copy link
Contributor Author

Hi there! Is it really not possible to use mudder 1.1.1 with Jest?

@vitaliytv
Copy link
Contributor

Hi there! Is it really not possible to use mudder 1.1.1 with Jest?

no, use 1.1.0

@fasiha
Copy link
Owner

fasiha commented Mar 13, 2022

@tiagocpeixoto are you trying to avoid pinning to a patch version of mudder in your package.json? If so, what I can do is re-release 1.1.1 as 2.0 and release a new 1.1.2 that's equal to 1.1.0 (i.e., roll back 1.1.1 in v1). I do think that, since we broke "developer" backwards compatibility somehow, we should release a new major version 😅.

Will that work for you? Please accept my apologies for (1) not realizing the issue with Jest (I'm not a user), and (2) for taking so long to understand your issue! Let me know if this will work, I'll take care of it right away (~24 hours).

@tiagocpeixoto
Copy link
Contributor Author

@tiagocpeixoto are you trying to avoid pinning to a patch version of mudder in your package.json? If so, what I can do is re-release 1.1.1 as 2.0 and release a new 1.1.2 that's equal to 1.1.0 (i.e., roll back 1.1.1 in v1). I do think that, since we broke "developer" backwards compatibility somehow, we should release a new major version 😅.

Will that work for you? Please accept my apologies for (1) not realizing the issue with Jest (I'm not a user), and (2) for taking so long to understand your issue! Let me know if this will work, I'll take care of it right away (~24 hours).

@fasiha, yes, it will work. Thank you in advance.

fasiha added a commit that referenced this issue Mar 13, 2022
@fasiha fasiha closed this as completed in bcc5068 Mar 13, 2022
@fasiha
Copy link
Owner

fasiha commented Mar 13, 2022

@tiagocpeixoto thanks for your patience, v1.1.2 should be identical to v1.1.0 and should continue working with Jest. v2.0 meanwhile has @vitaliytv's esbuild improvements. Both just pushed.

I hope we can figure out how to get Jest to work but in the meantime, hopefully this workaround is ok.

@tiagocpeixoto
Copy link
Contributor Author

Just a note: Jest v28 solves the problem and can be used with mudder v2.0.0.

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

3 participants