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

Broken conversion from tape tests #123

Open
chrvadala opened this issue Jul 28, 2018 · 2 comments
Open

Broken conversion from tape tests #123

chrvadala opened this issue Jul 28, 2018 · 2 comments

Comments

@chrvadala
Copy link

Hi guys,
I think that the conversion from tape doesn't work, because the tape t.plan(N) function and expect.assertions(N) have different behaviours; Moreover, Jest has this different way to work with callbacks https://jestjs.io/docs/en/asynchronous.html#callbacks

TAPE TEST

var test = require('tape');

test('', t => {
	t.plan(2)

	setTimeout(() => {
		t.ok(true)		
	}, 4000)

        setTimeout(() => {
		t.ok(true)
	}, 3000)
})

TAPE LOG

TAP version 13
# (anonymous)
ok 1 should be truthy
ok 2 should be truthy
1..2
# tests 2
# pass  2
# ok

JEST TEST (CONVERTED)

test('', () => {
	expect.assertions(2)
	
	setTimeout(() => {
		expect(true).toBeTruthy()		
	}, 4000)

	setTimeout(() => {
		expect(true).toBeTruthy()
	}, 3000)
})

JEST LOG

Expected two assertions to be called but received zero assertion calls.

      1 | test('', () => {
    > 2 | 	expect.assertions(2)
        | 	       ^
      3 | 	
      4 | 	setTimeout(() => {
      5 | 		expect(true).toBeTruthy()		

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
@skovhus
Copy link
Owner

skovhus commented Jul 29, 2018

Thanks for reporting this. It seems we are not handling that corner case correctly. : /

Let me know if you have any suggestions on how to fix this. And if you have time, then PRs are more than welcome. :)

@chrvadala
Copy link
Author

I think that the problem is that Jest and Tape work in two completely different ways. Probably a conversion from Tape to Jest requires more than just a methods conversions.
In a Tape test the number of assertions is planned and it waits until all the assertions are touched or timeout.
Jest doesn't wait for callbacks completion, but uses the assertions number just as check at the end of the test. It can be forced to wait using a done() callback, but the wait logic is up to the developer.
A possible conversion could be the following, but I'm not sure that it can fit any requirements.

test('', done => {
  const PLAN = 2
  let i = 0
  let touch = () => { if (++i >= PLAN) done() }
  expect.assertions(PLAN)

  setTimeout(() => {
    expect(true).toBeTruthy()
    touch()
  }, 4000)

  setTimeout(() => {
    expect(true).toBeTruthy()
    touch()
  }, 3000)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants