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

AVA t.throws results in failing test #1389

Closed
souldreamer opened this issue Nov 14, 2017 · 3 comments
Closed

AVA t.throws results in failing test #1389

souldreamer opened this issue Nov 14, 2017 · 3 comments

Comments

@souldreamer
Copy link

Issue description or question

Wallaby reports error when error is expected to actually pass the test using ava's t.throws, i.e.:

test('on a local gopher GET #open + #send throws a NetworkError', async t => {
	const xhr = t.context.xhr;
	t.throws(() => {
		xhr.open('GET', 'gopher:localhost:8911');
		xhr.send();
	}, XMLHttpRequest.NetworkError);
});

results in error (and test not passing):

test/xhr.spec.ts  on a local gopher GET #open + #send throws a NetworkError [2 ms]

	Error {
	  message: 'Unsupported protocol gopher:',
	}

Wallaby.js configuration file

module.exports = function (wallaby) {
	return {
		files: ['**/*.ts', '*.ts', '!test/**/*'],
		tests: ['test/**/*.ts'],
		env: {type: 'node'},
		testFramework: 'ava',
		recycle: true,
		name: 'XMLHttpRequest 2+',
		slowTestThreshold: 300,
		reportUnhandledPromises: false,
		workers: {
			initial: 1,
			regular: 1,
			recycle: true
		}
	}
};

Code editor or IDE name and version

WebStorm 2017.3 EAP
Build #WS-173.3622.29, built on November 9, 2017
WebStorm EAP User
Expiration date: December 9, 2017
JRE: 1.8.0_152-release-1024-b6 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

OS name and version

Mac OS X 10.13.1

@ArtemGovorov
Copy link
Member

ArtemGovorov commented Nov 14, 2017

I don't think the error is Wallaby specific, this is the way TypeScript compilation works (and the way AVA checks error types). You should get the same error when running AVA without wallaby.

As the error message says:

Error {
	  message: 'Unsupported protocol gopher:',
	}

AVA thinks that the error type is Error, however you're expecting it to be XMLHttpRequest.NetworkError. AVA is using instanceof to check the expected error type, however after TypeScript compilation, new NetworkError('an error') instanceof NetworkError is actually false, for the reasons described here:
microsoft/TypeScript#13965
microsoft/TypeScript#12790

You may easily see what I mean by running an example:

class NetworkError extends Error {
}

test('simple test', async t => {

    console.log((new NetworkError('an error') instanceof NetworkError)); // <-- prints false

    t.throws(() => {
        throw new NetworkError('an error')
    }, NetworkError);
});

A workaround would be to check the error message (you may also use a RegExp in AVA AFAIK):

    t.throws(() => {
        throw new NetworkError('an error')
    }, 'an error');

Hope it makes sense and answers your question.

@souldreamer
Copy link
Author

I was using ava-ts to run the tests when not using wallaby, and that variant worked. The actual problem was that I was checking against XMLHttpRequest.NetworkError and throwing a NetworkError (the XMLHttpRequest class has a static NetworkError = NetworkError property). Typescript knows they're the same, but after going through transpilation they ended up different classes. Actually throwing an XMLHttpRequest.NetworkError works.

Thanks for the help towards resolving my issue, the error message was very confusing (though I'm guessing that's ava rather than wallaby).

@ArtemGovorov
Copy link
Member

Awesome, thanks for the update!

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