tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test.ts (7167B)


      1 /**
      2 * @license
      3 * Copyright 2022 Google Inc.
      4 * SPDX-License-Identifier: Apache-2.0
      5 */
      6 import assert from 'node:assert/strict';
      7 import {describe, it} from 'node:test';
      8 
      9 import type {Platform, TestExpectation, MochaTestResult} from './types.js';
     10 import {
     11  filterByParameters,
     12  getTestResultForFailure,
     13  isWildCardPattern,
     14  testIdMatchesExpectationPattern,
     15  getExpectationUpdates,
     16 } from './utils.js';
     17 import {getFilename, extendProcessEnv} from './utils.js';
     18 
     19 describe('extendProcessEnv', () => {
     20  it('should extend env variables for the subprocess', () => {
     21    const env = extendProcessEnv([{TEST: 'TEST'}, {TEST2: 'TEST2'}]);
     22    assert.equal(env['TEST'], 'TEST');
     23    assert.equal(env['TEST2'], 'TEST2');
     24  });
     25 });
     26 
     27 describe('getFilename', () => {
     28  it('extract filename for a path', () => {
     29    assert.equal(getFilename('/etc/test.ts'), 'test');
     30    assert.equal(getFilename('/etc/test.js'), 'test');
     31  });
     32 });
     33 
     34 describe('getTestResultForFailure', () => {
     35  it('should get a test result for a mocha failure', () => {
     36    assert.equal(
     37      getTestResultForFailure({err: {code: 'ERR_MOCHA_TIMEOUT'}}),
     38      'TIMEOUT',
     39    );
     40    assert.equal(getTestResultForFailure({err: {code: 'ERROR'}}), 'FAIL');
     41  });
     42 });
     43 
     44 describe('filterByParameters', () => {
     45  it('should filter a list of expectations by parameters', () => {
     46    const expectations: TestExpectation[] = [
     47      {
     48        testIdPattern:
     49          '[oopif.spec] OOPIF "after all" hook for "should keep track of a frames OOP state"',
     50        platforms: ['darwin'],
     51        parameters: ['firefox', 'headless'],
     52        expectations: ['FAIL'],
     53      },
     54    ];
     55    assert.equal(
     56      filterByParameters(expectations, ['firefox', 'headless']).length,
     57      1,
     58    );
     59    assert.equal(filterByParameters(expectations, ['firefox']).length, 0);
     60    assert.equal(
     61      filterByParameters(expectations, ['firefox', 'headless', 'other']).length,
     62      1,
     63    );
     64    assert.equal(filterByParameters(expectations, ['other']).length, 0);
     65  });
     66 });
     67 
     68 describe('isWildCardPattern', () => {
     69  it('should detect if an expectation is a wildcard pattern', () => {
     70    assert.equal(isWildCardPattern(''), false);
     71    assert.equal(isWildCardPattern('a'), false);
     72    assert.equal(isWildCardPattern('*'), true);
     73 
     74    assert.equal(isWildCardPattern('[queryHandler.spec]'), false);
     75    assert.equal(isWildCardPattern('[queryHandler.spec] *'), true);
     76    assert.equal(isWildCardPattern(' [queryHandler.spec] '), false);
     77 
     78    assert.equal(isWildCardPattern('[queryHandler.spec] Query'), false);
     79    assert.equal(isWildCardPattern('[queryHandler.spec] Page *'), true);
     80    assert.equal(
     81      isWildCardPattern('[queryHandler.spec] Page Page.goto *'),
     82      true,
     83    );
     84  });
     85 });
     86 
     87 describe('testIdMatchesExpectationPattern', () => {
     88  const expectations: Array<[string, boolean]> = [
     89    ['', false],
     90    ['*', true],
     91    ['* should work', true],
     92    ['* Page.setContent *', true],
     93    ['* should work as expected', false],
     94    ['Page.setContent *', false],
     95    ['[page.spec]', false],
     96    ['[page.spec] *', true],
     97    ['[page.spec] Page *', true],
     98    ['[page.spec] Page Page.setContent *', true],
     99    ['[page.spec] Page Page.setContent should work', true],
    100    ['[page.spec] Page * should work', true],
    101    ['[page.spec] * Page.setContent *', true],
    102    ['[jshandle.spec] *', false],
    103    ['[jshandle.spec] JSHandle should work', false],
    104  ];
    105 
    106  it('with MochaTest', () => {
    107    const test = {
    108      title: 'should work',
    109      file: 'page.spec.ts',
    110      fullTitle() {
    111        return 'Page Page.setContent should work';
    112      },
    113    };
    114 
    115    for (const [pattern, expected] of expectations) {
    116      assert.equal(
    117        testIdMatchesExpectationPattern(test, pattern),
    118        expected,
    119        `Expected "${pattern}" to yield "${expected}"`,
    120      );
    121    }
    122  });
    123 
    124  it('with MochaTestResult', () => {
    125    const test: MochaTestResult = {
    126      title: 'should work',
    127      file: 'page.spec.ts',
    128      fullTitle: 'Page Page.setContent should work',
    129    };
    130 
    131    for (const [pattern, expected] of expectations) {
    132      assert.equal(
    133        testIdMatchesExpectationPattern(test, pattern),
    134        expected,
    135        `Expected "${pattern}" to yield "${expected}"`,
    136      );
    137    }
    138  });
    139 });
    140 
    141 describe('getExpectationUpdates', () => {
    142  it('should generate an update for expectations if a test passed with a fail expectation', () => {
    143    const mochaResults = {
    144      stats: {tests: 1},
    145      pending: [],
    146      passes: [
    147        {
    148          fullTitle: 'Page Page.setContent should work',
    149          title: 'should work',
    150          file: 'page.spec.ts',
    151        },
    152      ],
    153      failures: [],
    154    };
    155    const expectations = [
    156      {
    157        testIdPattern: '[page.spec] Page Page.setContent should work',
    158        platforms: ['darwin'] as Platform[],
    159        parameters: ['test'],
    160        expectations: ['FAIL' as const],
    161      },
    162    ];
    163    const updates = getExpectationUpdates(
    164      mochaResults,
    165      expectations,
    166      {
    167        platforms: ['darwin'] as Platform[],
    168        parameters: ['test'],
    169      },
    170      false,
    171    );
    172    assert.deepEqual(updates, [
    173      {
    174        action: 'remove',
    175        basedOn: {
    176          expectations: ['FAIL'],
    177          parameters: ['test'],
    178          platforms: ['darwin'],
    179          testIdPattern: '[page.spec] Page Page.setContent should work',
    180        },
    181        expectation: {
    182          expectations: ['FAIL'],
    183          parameters: ['test'],
    184          platforms: ['darwin'],
    185          testIdPattern: '[page.spec] Page Page.setContent should work',
    186        },
    187      },
    188    ]);
    189  });
    190 
    191  it('should not generate an update for expectations if a test passed with a fail expectation when passing are ingored', () => {
    192    const mochaResults = {
    193      stats: {tests: 1},
    194      pending: [],
    195      passes: [
    196        {
    197          fullTitle: 'Page Page.setContent should work',
    198          title: 'should work',
    199          file: 'page.spec.ts',
    200        },
    201      ],
    202      failures: [],
    203    };
    204    const expectations = [
    205      {
    206        testIdPattern: '[page.spec] Page Page.setContent should work',
    207        platforms: ['darwin'] as Platform[],
    208        parameters: ['test'],
    209        expectations: ['FAIL' as const],
    210      },
    211    ];
    212    const updates = getExpectationUpdates(
    213      mochaResults,
    214      expectations,
    215      {
    216        platforms: ['darwin'] as Platform[],
    217        parameters: ['test'],
    218      },
    219      true,
    220    );
    221    assert.deepEqual(updates, []);
    222  });
    223 
    224  it('should not generate an update for successful retries', () => {
    225    const mochaResults = {
    226      stats: {tests: 1},
    227      pending: [],
    228      passes: [
    229        {
    230          fullTitle: 'Page Page.setContent should work',
    231          title: 'should work',
    232          file: 'page.spec.ts',
    233        },
    234      ],
    235      failures: [
    236        {
    237          fullTitle: 'Page Page.setContent should work',
    238          title: 'should work',
    239          file: 'page.spec.ts',
    240          err: {code: 'Timeout'},
    241        },
    242      ],
    243    };
    244    const updates = getExpectationUpdates(
    245      mochaResults,
    246      [],
    247      {
    248        platforms: ['darwin'],
    249        parameters: ['test'],
    250      },
    251      false,
    252    );
    253    assert.deepEqual(updates, []);
    254  });
    255 });