Skip to content

Commit

Permalink
[Tests] test for --no-only flag present/absent
Browse files Browse the repository at this point in the history
  • Loading branch information
jocrah committed Jan 23, 2022
1 parent 988f1c3 commit 91a9108
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
121 changes: 121 additions & 0 deletions test/no_only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
'use strict';

var tap = require('tap');
var path = require('path');
var spawn = require('child_process').spawn;
var concat = require('concat-stream');

var stripFullStack = require('./common').stripFullStack;

var tapeBin = path.join(process.cwd(), 'bin/tape');

tap.test(
'Should throw error when --no-only is passed via cli and there is a .only test',
{ todo: process.versions.node.match(/0\.10\.\d+/) ? 'Fails on node 0.10' : false },
function (tt) {
tt.plan(2);

var testStderr = function (rows) {
tt.ok((/Error: `only` tests are prohibited\n/).test(stripFullStack(rows.toString('utf8')).join('\n')));
};

var ps = spawn(tapeBin, ['**/*.js', '--no-only' ], { cwd: path.join(__dirname, 'no_only') });
ps.stderr.pipe(concat(testStderr));
ps.on('exit', function (code) {
tt.equal(code, 1);
});
}
);

tap.test(
'Should throw error when NODE_TAPE_NO_ONLY_TEST is passed via envs and there is an .only test',
{ todo: process.versions.node.match(/0\.10\.\d+/) ? 'Fails on node 0.10' : false },
function (tt) {
tt.plan(2);

var testStderr = function (rows) {
tt.ok((/Error: `only` tests are prohibited\n/).test(stripFullStack(rows.toString('utf8')).join('\n')));
};
var ps = spawn(tapeBin, ['**/*.js'], {
cwd: path.join(__dirname, 'no_only'),
env: { NODE_TAPE_NO_ONLY_TEST: true }
});
ps.stderr.pipe(concat(testStderr));
ps.on('exit', function (code) {
tt.equal(code, 1);
});
}
);

tap.test(
'Should override NODE_TAPE_NO_ONLY_TEST env if --no-only is passed from cli',
{ todo: process.versions.node.match(/0\.10\.\d+/) ? 'Fails on node 0.10' : false },
function (tt) {
tt.plan(2);

var testStderr = function (rows) {
tt.ok((/Error: `only` tests are prohibited\n/).test(stripFullStack(rows.toString('utf8')).join('\n')));
};

var ps = spawn(tapeBin, ['**/*.js', '--no-only' ], {
cwd: path.join(__dirname, 'no_only'),
env: { NODE_TAPE_NO_ONLY_TEST: false }
});
ps.stderr.pipe(concat(testStderr));
ps.on('exit', function (code) {
tt.equal(code, 1);
});
}
);

tap.test('Should run successfully if there is no only test', function (tt) {
tt.plan(2);

var testStdout = function (rows) {
tt.same(stripFullStack(rows.toString('utf8')), [
'TAP version 13',
'# should pass',
'ok 1 should be truthy',
'',
'1..1',
'# tests 1',
'# pass 1',
'',
'# ok',
'',
''
]);
};

var ps = spawn(tapeBin, ['**/test-a.js', '--no-only'], { cwd: path.join(__dirname, 'no_only') });
ps.stdout.pipe(concat(testStdout));
ps.on('exit', function (code) {
tt.equal(code, 0);
});
});

tap.test('Should run successfully if there is an only test and no --no-only flag', function (tt) {
tt.plan(2);

var testStdout = function (rows) {
tt.same(stripFullStack(rows.toString('utf8')), [
'TAP version 13',
'# should pass again',
'ok 1 should be truthy',
'',
'1..1',
'# tests 1',
'# pass 1',
'',
'# ok',
'',
''
]);
};

var ps = spawn(tapeBin, ['**/test-b.js'], { cwd: path.join(__dirname, 'no_only') });
ps.stdout.pipe(concat(testStdout));
ps.on('exit', function (code) {
tt.equal(code, 0);
});
});
8 changes: 8 additions & 0 deletions test/no_only/test-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

var tape = require('../../');

tape.test('should pass', function (t) {
t.plan(1);
t.ok(1);
});
14 changes: 14 additions & 0 deletions test/no_only/test-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var tape = require('../../');

tape.test('should pass', function (t) {
t.plan(1);
t.ok(1);
});

tape.test.only('should pass again', function (t) {
t.plan(1);
t.ok(1);
});

0 comments on commit 91a9108

Please sign in to comment.