-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
no-datenow-test.js
103 lines (91 loc) · 3.54 KB
/
no-datenow-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const DateNowUseAudit = require('../../../audits/dobetterweb/no-datenow.js');
const assert = require('assert');
const URL = 'https://example.com';
/* eslint-env mocha */
describe('Page does not use Date.now()', () => {
it('passes when Date.now() is not used', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: [],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, true);
assert.equal(auditResult.extendedInfo.value.results.length, 0);
});
it('passes when Date.now() is used on a different origin', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: [
{url: 'http://different.com/two', line: 2, col: 2},
{url: 'http://example2.com/two', line: 2, col: 22}
],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, true);
assert.equal(auditResult.extendedInfo.value.results.length, 0);
});
it('fails when Date.now() is used on the origin', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: [
{url: 'http://example.com/one', line: 1, col: 1},
{url: 'http://example.com/two', line: 10, col: 1},
{url: 'http://example2.com/two', line: 2, col: 22}
],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.results.length, 2);
const headings = auditResult.extendedInfo.value.tableHeadings;
assert.deepEqual(Object.keys(headings).map(key => headings[key]),
['URL', 'Line/Col', 'Eval\'d?'], 'table headings are correct and in order');
});
it('only passes when has url property', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: [
{url: 'http://example.com/two', line: 10, col: 1},
{url: 'http://example2.com/two', line: 2, col: 22}
],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.results.length, 1);
});
it('fails when console.time() is used in eval()', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: [
{url: 'http://example.com/one', line: 1, col: 1, isEval: false},
{url: 'module.exports (blah/handler.js:5:18)', line: 5, col: 18, isEval: true},
{url: 'module.exports (blah/handler.js:3:18)', line: 3, col: 18, isEval: true}
],
URL: {finalUrl: URL}
});
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.results.length, 3);
});
it('includes results when there is no .url', () => {
const auditResult = DateNowUseAudit.audit({
DateNowUse: [
{line: 10, col: 1},
{line: 2, col: 22}
],
URL: {finalUrl: URL},
});
assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.extendedInfo.value.results.length, 2);
assert.ok(auditResult.debugString, 'includes debugString');
});
});