Chai: Trying to make 'should' work

Created on 2 Jan 2017  ·  8Comments  ·  Source: chaijs/chai

I'm trying to make 'should' work, but it's undefined. I got 'expect' working perfectly.

Good job on the project! Just need help getting started http://stackoverflow.com/questions/41422101/testing-with-chai-should-exist-not-working-with-async

All 8 comments

Hello @TylerL-uxai and happy holidays. Thanks for reaching out.

If you are on npm version, you have to require('chai').should() or

import chai from 'chai'
chai.should()

Please, note the function call. I had issues with getting Object#should to work too when first started using Chai.

If you are on master (not canary), you can also do import 'chai/register-should' or with Mocha:

$ mocha specs -r chai/register-should -w

Thank you for the fast response! I think my problem is with async callbacks (hopefully I can use this). My question got 42 views and two upvotes on StackOverflow http://stackoverflow.com/questions/41435074/how-to-get-the-callback-of-an-async-function-in-javascript

Maybe you can answer it? I promise to do a brief writeup on unit testing to react-starter-kit, which should generate some traffic for the chai framework as well.

Side note: I have to compliment your chai website. It's so nice looking!

Try

import { expect } from 'chai';
import { Customer } from '../../data/models';

describe('Customers Server', () => {
  it('Create a customer', async () => {
    const createCustomer = await Customer.create({ email: '[email protected]' });
    expect(createCustomer).to.exist;
  });
});

Async is built in to expect! Great!

Thanks :) That totally worked

You are welcome.

Is there any way to get rid of the eslint error?
“no-unused-expressions” Expected an assignment or function call and instead saw an expression.

Edit: Note that there's no error when I convert 'exist' to exist(). This breaks the test, but fixes the eslint error.

expect(leftOverCustomers).to.not.exist();

screen shot 2017-01-03 at 6 01 45 pm

You'll either need to turn that rule off within a eslint config located in your test directory, or check out something like https://github.com/prodatakey/dirty-chai

Thank you, meeber! I included a working example of some tests on React-Starter-Kit.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

endymion00 picture endymion00  ·  3Comments

huaguzheng picture huaguzheng  ·  3Comments

zzzgit picture zzzgit  ·  3Comments

JuHwon picture JuHwon  ·  5Comments

leifhanack picture leifhanack  ·  4Comments