jest icon indicating copy to clipboard operation
jest copied to clipboard

[Feature]: Extensible, type-friendly and explicit test/expect API

Open marionebl opened this issue 3 years ago • 4 comments

🚀 Feature Proposal

Take a leaf out of playwright's playbook and expose an explicit, extensible test and expect API. Include the fixture concept to help factor setup and cleanup code out of tests files in a browsable, type safe and explicit way. This follows the prior art established by playwright: https://playwright.dev/docs/api/class-fixtures

Motivation

Doubling down on being type system friendly is a good investment in the JavaScript ecosystem of 2022. Providing test authors with tools to keep tests focused will lead to better test suites. Thi

Example

In its simplest form this shifts jest usage from relying on globals to explicitly imported functions

import { test } from '@jest/test';

test('1 + 1 = 2', () =>  {
  expect(1 + 1).toBe(2);
});

It also affords a way for test suite authors to provide fixtures in a standardised and easily reusable way

// browser-tests.ts
import { test as base } from '@jest/test';
export { expect } from '@jest/test';

export const test = base.extend({
   browser: (use) => {
     const browser = setup(/* ... */);
     await use(browser)
     await teardown(browser);
   }
});
// tests.ts
import { test, expect } from '../browser-tests.ts';

test('1 + 1 = 2 in a browser', async ({  browser }) =>  {
   const result = await browser.evaluate(() => 1 + 1);
   expect(result).toBe(2);
});

Pitch

Belongs into core as it touches on a low level how jest test suites are structured.

marionebl avatar Sep 03 '22 08:09 marionebl

In its simplest form this shifts jest usage from relying on globals to explicitly imported functions

That's already possible via @jest/globals: https://jestjs.io/blog/2020/05/05/jest-26#a-new-way-to-consume-jest---jestglobals (the typescript caveat is no longer true: https://jestjs.io/docs/next/expect#expectextendmatchers)


However, the part about teardown and stuff looks interesting!

SimenB avatar Sep 03 '22 11:09 SimenB

Hi @SimenB would love working on the feature request.

Parth0105 avatar Sep 03 '22 13:09 Parth0105

That's already possible via @jest/globals: https://jestjs.io/blog/2020/05/05/jest-26#a-new-way-to-consume-jest---jestglobals (the typescript caveat is no longer true: https://jestjs.io/docs/next/expect#expectextendmatchers)

Thanks for the pointer at @jest/globals - I was behind the times! :)

marionebl avatar Sep 04 '22 03:09 marionebl

I suggested something like this way back when: https://github.com/facebook/jest/pull/8654#issuecomment-510385442 (see bottom of that comment) - that looks pretty much like what base.extend in the OP is.


Hey @Parth0105, thanks for offering! While I'm keen on getting something like what is suggested here, I think we'll need to go through a longer design phase before we start writing any code. And I don't know when I'll have the time and/or energy for that.

However, if you're up for it, I'd love to review some code. But I cannot provide any guarantees it'll land, so it might be a bit of a waste of time for you 🙂

SimenB avatar Sep 09 '22 11:09 SimenB

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Oct 09 '22 12:10 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Nov 08 '22 12:11 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Nov 08 '22 12:11 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Dec 09 '22 00:12 github-actions[bot]