123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- 'use strict';
- require('../lib/chalk').set();
- require('../lib/worker/options').set({color: false});
- const Promise = require('bluebird');
- const test = require('tap').test;
- const Test = require('../lib/test');
- function ava(fn) {
- return new Test({
- contextRef: null,
- failWithoutAssertions: true,
- fn,
- metadata: {type: 'test', callback: false},
- title: '[anonymous]'
- });
- }
- ava.cb = function (fn) {
- return new Test({
- contextRef: null,
- failWithoutAssertions: true,
- fn,
- metadata: {type: 'test', callback: true},
- title: '[anonymous]'
- });
- };
- function pass() {
- return new Promise(resolve => {
- setImmediate(resolve);
- });
- }
- function fail() {
- return new Promise((resolve, reject) => {
- setImmediate(() => {
- reject(new Error('unicorn'));
- });
- });
- }
- test('returning a promise from a legacy async fn is an error', t => {
- return ava.cb(a => {
- a.plan(1);
- return Promise.resolve(true).then(() => {
- a.pass();
- a.end();
- });
- }).run().then(result => {
- t.is(result.passed, false);
- t.match(result.error.message, /Do not return promises/);
- });
- });
- test('assertion plan is tested after returned promise resolves', t => {
- const start = Date.now();
- const instance = ava(a => {
- a.plan(2);
- const defer = Promise.defer();
- setTimeout(() => {
- defer.resolve();
- }, 500);
- a.pass();
- a.pass();
- return defer.promise;
- });
- return instance.run().then(result => {
- t.is(result.passed, true);
- t.is(instance.planCount, 2);
- t.is(instance.assertCount, 2);
- t.true(Date.now() - start >= 500);
- });
- });
- test('missing assertion will fail the test', t => {
- return ava(a => {
- a.plan(2);
- const defer = Promise.defer();
- setTimeout(() => {
- a.pass();
- defer.resolve();
- }, 200);
- return defer.promise;
- }).run().then(result => {
- t.is(result.passed, false);
- t.is(result.error.assertion, 'plan');
- });
- });
- test('extra assertion will fail the test', t => {
- return ava(a => {
- a.plan(2);
- const defer = Promise.defer();
- setTimeout(() => {
- a.pass();
- a.pass();
- }, 200);
- setTimeout(() => {
- a.pass();
- defer.resolve();
- }, 500);
- return defer.promise;
- }).run().then(result => {
- t.is(result.passed, false);
- t.is(result.error.assertion, 'plan');
- });
- });
- test('assert pass', t => {
- const instance = ava(a => {
- return pass().then(() => {
- a.pass();
- });
- });
- return instance.run().then(result => {
- t.is(result.passed, true);
- t.is(instance.assertCount, 1);
- });
- });
- test('assert fail', t => {
- return ava(a => {
- return pass().then(() => {
- a.fail();
- });
- }).run().then(result => {
- t.is(result.passed, false);
- t.is(result.error.name, 'AssertionError');
- });
- });
- test('reject', t => {
- return ava(a => {
- return fail().then(() => {
- a.pass();
- });
- }).run().then(result => {
- t.is(result.passed, false);
- t.is(result.error.name, 'AssertionError');
- t.is(result.error.message, 'Rejected promise returned by test');
- t.is(result.error.values.length, 1);
- t.is(result.error.values[0].label, 'Rejected promise returned by test. Reason:');
- t.match(result.error.values[0].formatted, /.*Error.*\n.*message: 'unicorn'/);
- });
- });
- test('reject with non-Error', t => {
- return ava(() => {
- return Promise.reject('failure'); // eslint-disable-line prefer-promise-reject-errors
- }).run().then(result => {
- t.is(result.passed, false);
- t.is(result.error.name, 'AssertionError');
- t.is(result.error.message, 'Rejected promise returned by test');
- t.is(result.error.values.length, 1);
- t.is(result.error.values[0].label, 'Rejected promise returned by test. Reason:');
- t.match(result.error.values[0].formatted, /failure/);
- });
- });
|