-
Notifications
You must be signed in to change notification settings - Fork 7
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
expectBody not verbose enough #16
Comments
I just tried cloning a fresh copy
And then I added this to test/test.ts: it('should generate a nice diff if body is incorrect', async function() {
await fetch(this.server, '/hello')
.expectStatus(200)
.expectHeader('content-type', 'application/json')
.expectBody({greeting: "Hello2!"});
}); And when I
supertest-fetch is just using straight up > assert = require('assert');
> let err;
> try { assert.deepStrictEqual({a: "foo"}, {a: "bar"}) } catch(e) { err = e; } And then have a look at So, I guess the first question is, what test runner are you using? I guess Jest? I've never used it, but at a glance Jest is supposed to handle this. |
Indeed I'm using jest and I'm not getting a smart output. Tried with your project using jest + ts-jest: yarn add --dev jest
yarn add --dev ts-jest With the following module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/?(*.)+(spec|test).(j|t)s?(x)'],
setupFiles: ['<rootDir>/test/setup.js']
}; With this global.fetch = require('node-fetch'); And the following import http from 'http';
import { fetch } from '../../src';
const server = http.createServer(
(req, res) => {
if(req.url === '/hello') {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({greeting: "Hello!"}));
} else if(req.url === '/hellotext') {
res.end("Hello");
} else if(req.url === '/err') {
res.setHeader('content-type', req.headers['content-type'] || 'text/plain');
res.statusCode = 400;
res.end('Boom!\nLong message\n');
} else {
res.statusCode = 404;
res.end();
}
}
);
it('should generate a nice diff if body is incorrect', async function() {
await fetch(server, '/hello')
.expectStatus(200)
.expectHeader('content-type', 'application/json')
.expectBody({greeting: "Hello2!"});
}); Running with |
If you want another quick test, try writing a jest test that just does something like:
and see what that does. If that doesn't produce a pretty diff for you, I'd suggest this is something you should raise with the jest project. |
Does seem to work fine with jest, in another project: it.only('test', () => {
require('assert').deepStrictEqual({a: 'foo'}, {a: 'bar'});
}); Gives me:
|
I believe the technical term I'm looking for is "wat!?" This is exactly what we're doing in supertest-fetch. -_- |
I'm playing with the source to see if I can pinpoint the diff, very strange indeed:
EDIT the Jest error formatter is here: https://github.com/facebook/jest/blob/master/packages/jest-circus/src/format_node_assert_errors.js Can't really find what might be going on. |
When reading the docs, we could expect:
to have the same behavior than:
However these two examples give very different outputs:
vs.
Not sure if this is easily doable but having the actual expect error thrown in the first case would be great as it's quite impractical for now.
Thanks for the great lib!
EDIT I realized that this was due to the fact that I'm using jest version of expect. Since the package is quite small, I think it would be great to rely on it to test assertions. As you'll get nice diffed output.
The text was updated successfully, but these errors were encountered: