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 Runner: In-source testing #45771

Closed
nickserv opened this issue Dec 7, 2022 · 7 comments
Closed

Test Runner: In-source testing #45771

nickserv opened this issue Dec 7, 2022 · 7 comments
Labels
feature request Issues that request new features to be added to Node.js. test_runner Issues and PRs related to the test runner subsystem.

Comments

@nickserv
Copy link

nickserv commented Dec 7, 2022

What is the problem this feature will solve?

In-source tests let you write some tests in the same files as their implementation.

Vitest's explanation:

This makes the tests share the same closure as the implementations and able to test against private states without exporting. Meanwhile, it also brings a closer feedback loop for development.

Building this into Node would also have the advantage of letting tutorials and examples for Node users show tests in the same files.

What is the feature you are proposing to solve the problem?

A --test-source flag that will run any Node files and expose the test function via import.meta.test (as importing it could cause it to be accidentally bundled).

if (import.meta.test) {
  import.meta.test("Array.indexOf()", () => {
    assert.strictEqual([1, 2, 3].indexOf(4), -1)
  })
}

The rest of the node:test module could also be exposed as properties of the import.meta.test function.

if (import.meta.test) {
  const { describe, it } = import.meta.test
  describe("Array", () => {
    describe("#indexOf()", () => {
      it("should return -1 when the value is not present", () => {
        assert.strictEqual([1, 2, 3].indexOf(4), -1)
      })
    })
  })
}

What alternatives have you considered?

@nickserv nickserv added the feature request Issues that request new features to be added to Node.js. label Dec 7, 2022
@MoLow MoLow added the test_runner Issues and PRs related to the test runner subsystem. label Dec 8, 2022
@MoLow
Copy link
Member

MoLow commented Dec 8, 2022

import.meta is an ESM thing, so if we decide to add this feature we can probably make it with an API that will work for both CJS and ESM:

const {inlineTest, describe, it} = require('node:test');
if (inlineTest) {
  describe("Array", () => {
    describe("#indexOf()", () => {
      it("should return -1 when the value is not present", () => {
        assert.strictEqual([1, 2, 3].indexOf(4), -1)
      })
    })
  })
}

@nickserv
Copy link
Author

nickserv commented Dec 8, 2022

This would import the whole module, though. I was trying to avoid that, and it's the reason Vitest has a similar API (though it's ESM only). Perhaps we could use properties on require or module for CJS?

@debadree25
Copy link
Member

So if I understand correctly if we ran node --test some-file-with-inline-test.mjs then the test module would get included in the import meta and the test suites should be run?

debadree25 added a commit to debadree25/node that referenced this issue Feb 13, 2023
@MoLow
Copy link
Member

MoLow commented Apr 24, 2023

I am closing as wont do, feel free to reopen if you intend to work on this

@MoLow MoLow closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2023
@nickserv
Copy link
Author

So you'd consider merging a prototype? I can give it a try if we can reach consensus on an import syntax, though it's be easier to discuss this if you could reopen the issue.

@MoLow MoLow reopened this Apr 24, 2023
@cjihrig
Copy link
Contributor

cjihrig commented May 10, 2023

@nickmccurdy do you have any update on this? If not, I propose closing the issue. That wouldn't stop you from working on it if you really want to see this happen.

@cjihrig
Copy link
Contributor

cjihrig commented May 24, 2023

No follow up for two weeks. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants