no_only.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. var tap = require('tap');
  3. var path = require('path');
  4. var exec = require('child_process').exec;
  5. var stripFullStack = require('./common').stripFullStack;
  6. var tapeBin = 'node ' + path.join(__dirname, '../bin/tape');
  7. var expectedExitCodeFailure = (/^0\.10\.\d+$/).test(process.versions.node);
  8. var expectedStackTraceBug = (/^3\.[012]\.\d+$/).test(process.versions.node); // https://github.com/nodejs/node/issues/2581
  9. tap.test(
  10. 'Should throw error when --no-only is passed via cli and there is a .only test',
  11. { todo: expectedExitCodeFailure || expectedStackTraceBug ? 'Fails on these node versions' : false },
  12. function (tt) {
  13. tt.plan(3);
  14. exec(tapeBin + ' --no-only "**/*.js"', {
  15. cwd: path.join(__dirname, 'no_only')
  16. }, function (err, stdout, stderr) {
  17. tt.same(stdout.toString('utf8'), '');
  18. tt.match(stripFullStack(stderr.toString('utf8')).join('\n'), /Error: `only` tests are prohibited\n/);
  19. tt.equal(err.code, 1);
  20. });
  21. }
  22. );
  23. tap.test(
  24. 'Should throw error when NODE_TAPE_NO_ONLY_TEST is passed via envs and there is an .only test',
  25. { todo: expectedExitCodeFailure || expectedStackTraceBug ? 'Fails on these node versions' : false },
  26. function (tt) {
  27. tt.plan(3);
  28. exec(tapeBin + ' "**/*.js"', {
  29. cwd: path.join(__dirname, 'no_only'),
  30. env: { PATH: process.env.PATH, NODE_TAPE_NO_ONLY_TEST: 'true' }
  31. }, function (err, stdout, stderr) {
  32. tt.same(stdout.toString('utf8'), '');
  33. tt.match(stripFullStack(stderr.toString('utf8')).join('\n'), /Error: `only` tests are prohibited\n/);
  34. tt.equal(err.code, 1);
  35. });
  36. }
  37. );
  38. tap.test(
  39. 'Should override NODE_TAPE_NO_ONLY_TEST env if --no-only is passed from cli',
  40. { todo: expectedExitCodeFailure || expectedStackTraceBug ? 'Fails on these node versions' : false },
  41. function (tt) {
  42. tt.plan(3);
  43. exec(tapeBin + ' --no-only "**/*.js"', {
  44. cwd: path.join(__dirname, 'no_only'),
  45. env: { PATH: process.env.PATH, NODE_TAPE_NO_ONLY_TEST: 'false' }
  46. }, function (err, stdout, stderr) {
  47. tt.same(stdout.toString('utf8'), '');
  48. tt.match(stripFullStack(stderr.toString('utf8')).join('\n'), /Error: `only` tests are prohibited\n/);
  49. tt.equal(err.code, 1);
  50. });
  51. }
  52. );
  53. tap.test('Should run successfully if there is no only test', function (tt) {
  54. tt.plan(3);
  55. exec(tapeBin + ' --no-only "**/test-a.js"', {
  56. cwd: path.join(__dirname, 'no_only')
  57. }, function (err, stdout, stderr) {
  58. tt.match(stderr.toString('utf8'), /^\s*(\(node:\d+\) ExperimentalWarning: The ESM module loader is experimental\.)?\s*$/);
  59. tt.same(stripFullStack(stdout.toString('utf8')), [
  60. 'TAP version 13',
  61. '# should pass',
  62. 'ok 1 should be truthy',
  63. '',
  64. '1..1',
  65. '# tests 1',
  66. '# pass 1',
  67. '',
  68. '# ok',
  69. '',
  70. ''
  71. ]);
  72. tt.equal(err, null); // code 0
  73. });
  74. });
  75. tap.test('Should run successfully if there is an only test and no --no-only flag', function (tt) {
  76. tt.plan(3);
  77. exec(tapeBin + ' "**/test-b.js"', {
  78. cwd: path.join(__dirname, 'no_only')
  79. }, function (err, stdout, stderr) {
  80. tt.same(stripFullStack(stdout.toString('utf8')), [
  81. 'TAP version 13',
  82. '# should pass again',
  83. 'ok 1 should be truthy',
  84. '',
  85. '1..1',
  86. '# tests 1',
  87. '# pass 1',
  88. '',
  89. '# ok',
  90. '',
  91. ''
  92. ]);
  93. tt.match(stderr.toString('utf8'), /^\s*(\(node:\d+\) ExperimentalWarning: The ESM module loader is experimental\.)?\s*$/);
  94. tt.equal(err, null); // code 0
  95. });
  96. });