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

test: replace jest with mocha #420

Merged
merged 7 commits into from
Oct 17, 2024
Merged

test: replace jest with mocha #420

merged 7 commits into from
Oct 17, 2024

Conversation

gjf7
Copy link
Contributor

@gjf7 gjf7 commented Oct 11, 2024

Closes #333.

To make a transition from Jest feasible. Capabilities Jest offers that we might need to keep:

Expect

Expect is a standalone npm package.

Mock

The jest.fn(), jest.spyOn() is provided by jest-mock, we can just use this package directly.

@gjf7
Copy link
Contributor Author

gjf7 commented Oct 11, 2024

@justinmk Mocha doesn't support coverage out of box, Do u think https://github.com/istanbuljs/nyc is a good choice for us?

@justinmk
Copy link
Member

@justinmk Mocha doesn't support coverage out of box, Do u think https://github.com/istanbuljs/nyc is a good choice for us?

nyc is deprecated, https://github.com/bcoe/c8 is the current best option AFAIK.

@gjf7 gjf7 force-pushed the jest-to-mocha branch 2 times, most recently from 457321d to ac97e71 Compare October 16, 2024 13:55
"test-build": "npm test --runInBand --coverage",
"test-staged": "npm test --bail --no-cache --findRelatedTests",
"test": "mocha --exit --parallel --require ts-node/register --require src/testSetup.ts src/**/*.test.ts",
"test-coverage": "c8 --reporter=json --reporter=html npm test",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test-build make no sense here, so I rename it to test-coverage

"test-staged": "npm test --bail --no-cache --findRelatedTests",
"test": "mocha --exit --parallel --require ts-node/register --require src/testSetup.ts src/**/*.test.ts",
"test-coverage": "c8 --reporter=json --reporter=html npm test",
"test-staged": "npm test --bail",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we still need this script command ?

Copy link
Member

@justinmk justinmk Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for husky or something like that.

node-client/package.json

Lines 44 to 49 in 4d6d4d3

"lint-staged": {
"*.{ts,js}": [
"eslint --fix",
"npm run test-staged"
]
},

I'd probably prefer to remove lint-staged and similar stuff. This repo is simple and should stay simple, so not worth having that lint-staged dep imo.

But probably off-topic for this PR.

@gjf7 gjf7 marked this pull request as ready for review October 16, 2024 13:56
@gjf7 gjf7 marked this pull request as draft October 16, 2024 14:03
@gjf7
Copy link
Contributor Author

gjf7 commented Oct 16, 2024

C8 requires node >= 18, wtf?

Copy link
Member

@justinmk justinmk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit worried about this but this looks like a win. You did it in a way that reduces risk and churn. Thank you!

"@types/node": "16.9.x",
"c8": "^10.1.2",
"expect": "^29.7.0",
"jest-mock": "^29.7.0",
Copy link
Member

@justinmk justinmk Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest-mock is fine as a temporary step (if it saves some noise in this PR), but maybe we want to switch to https://sinonjs.org/ ? Or maybe it doesn't matter. (But maybe it's confusing to have both jest and mocha :)

packages/neovim/package.json Outdated Show resolved Hide resolved
.eslintrc.js Outdated
@@ -70,6 +71,7 @@ module.exports = {

'import/extensions': 'off',
'import/prefer-default-export': 'off',
"import/no-extraneous-dependencies": ["error", {"devDependencies": true, "optionalDependencies": false, "peerDependencies": false}],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

single quotes

"testPathIgnorePatterns": [".d.ts", ".js"]
"@types/mocha": "^10.0.9",
"c8": "^10.1.2",
"expect": "^29.7.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK as a transitional step, but can probably be avoided in a later PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll handle this(replace expect and jest-mock with assert and sinon) in a follow up PR :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that although expect made by Jest, should we replace it ? or is it worth to do so(expect is a good assert lib in my opinion)?

// eslint-disable-next-line import/no-mutable-exports
export let proc: cp.ChildProcessWithoutNullStreams;
// eslint-disable-next-line import/no-mutable-exports
export let nvim: NeovimClient;
Copy link
Member

@justinmk justinmk Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can/should this be a function? it's a bit "magical" as a global var, and also makes debugging a bit more painful?

Not a blocker.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll handle this too in a follow up PR :)

packages/neovim/src/host/NvimPlugin.test.ts Outdated Show resolved Hide resolved
packages/neovim/src/host/NvimPlugin.test.ts Outdated Show resolved Hide resolved
@justinmk
Copy link
Member

C8 requires node >= 18, wtf?

We could skip codecov for older node.

@@ -155,7 +150,7 @@ describe('Nvim API', () => {
});

it('emits "disconnect" after quit', done => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't know why this test is keep failing!

Copy link
Member

@justinmk justinmk Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can skip it for now. even after the 'exit' workaround it still fails for me locally, sometimes.

#419

@gjf7 gjf7 marked this pull request as ready for review October 16, 2024 15:16
@gjf7
Copy link
Contributor Author

gjf7 commented Oct 16, 2024

looks like there is a Uncaught error in our test util.

@justinmk
Copy link
Member

justinmk commented Oct 16, 2024

maybe drop --parallel for now? not sure if these tests are prepared for that.

@gjf7
Copy link
Contributor Author

gjf7 commented Oct 16, 2024

maybe drop --parallel for now? not sure if these tests are prepared for that.

Drop --parallel make `process.env.NODE_ENV = 'test' not executed, don't know why.

Got error Exception during run: TypeError: Cannot read properties of undefined (reading 'parseVersion' NODE_ENV is undefined.

@justinmk
Copy link
Member

that's weird, but based on mochajs/mocha#185 seems like mocha intentionally does not set NODE_ENV? For now we could just add to testSetup.ts:

process.env.NODE_ENV = 'test';


import * as testUtil from './testUtil';
// import * as jest from '@jest/globals'
process.env.NODE_ENV = 'test';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already done that, I mean this line is not working after drop --parallel.

@gjf7
Copy link
Contributor Author

gjf7 commented Oct 16, 2024

Turns out we must set 'NODE_ENV=test' in .mocharc.js in non-parallel mode. Sets in testSetup is useless :)

@gjf7
Copy link
Contributor Author

gjf7 commented Oct 16, 2024

Need to check why codecov is't triggered.

@justinmk
Copy link
Member

justinmk commented Oct 16, 2024

might need a .c8rc.json in packages/neovim/. IIRC the "lcov" reporter was important to get it to work with codecov.

example (untested, just a sample):

{
    "report-dir": "../../coverage/neovim",
    "reporter": ["lcov"],
    "all": true,
    "include": ["src/**"],
    "exclude": [
        "src/test/**"
    ]
}

and the GHA workflow:

  uses: codecov/codecov-action@v4
  with:
      verbose: true
      file: ./coverage/neovim/lcov.info
      token: ${{ secrets.CODECOV_TOKEN }}

@@ -30,7 +30,7 @@
"dev": "npm run --stream --parallel dev --workspaces --if-present",
"publish:neovim": "cd packages/neovim && cp ../../README.md . && cp ../../CHANGELOG.md . && npm publish",
"test": "npm run test --workspaces --if-present",
"test-coverage": "npm run test --workspaces --if-present",
"test-coverage": "npm run test-coverage --workspaces --if-present",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad :), codecov works now.

@gjf7
Copy link
Contributor Author

gjf7 commented Oct 17, 2024

Follow up PR TODO:

  1. eliminate the magical global nvim, proc in test util.
  2. replace expect with Node.js built-in assert module to reduces total number of dependencies

.eslintrc.js Outdated Show resolved Hide resolved
@justinmk
Copy link
Member

justinmk commented Oct 17, 2024

replace jest-mock with sinon

On second thought, let's keep jest-mock for now. Mocking should be rare, and unless sinon improves something, using jest-mock avoids churn and doesn't really hurt.

keep expect is fine

reading https://jestjs.io/docs/expect , mocha (or assert) already provides this except the syntax is different? If removing expect reduces total number of dependencies, then it might be worth doing.

  1. eliminate the magical global nvim, proc in test util

I'm also not sure about this. The current situation looks ok, since this repo is very simple (and getting simpler, thanks to this PR :).

@gjf7
Copy link
Contributor Author

gjf7 commented Oct 17, 2024

Ready for merge :)

@justinmk justinmk merged commit 63fb96c into neovim:master Oct 17, 2024
10 checks passed
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

Successfully merging this pull request may close these issues.

tests: replace jest with mocha or something else
2 participants