This repository was archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathlinter-jshint-spec.js
164 lines (135 loc) · 5.96 KB
/
linter-jshint-spec.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use babel';
import {
// eslint-disable-next-line no-unused-vars
it, fit, wait, beforeEach, afterEach,
} from 'jasmine-fix';
import * as path from 'path';
import linter from '../lib/main';
const goodPath = path.join(__dirname, 'fixtures', 'good.js');
const bitwisePath = path.join(__dirname, 'fixtures', 'bitwise', 'bitwise.js');
async function getNotification(expectedMessage) {
return new Promise((resolve) => {
let notificationSub;
const newNotification = (notification) => {
if (notification.getMessage() !== expectedMessage) {
// As the specs execute asynchronously, it's possible a notification
// from a different spec was grabbed, if the message doesn't match what
// is expected simply return and keep waiting for the next message.
return;
}
// Dispose of the notificaiton subscription
notificationSub.dispose();
resolve(notification);
};
// Subscribe to Atom's notifications
notificationSub = atom.notifications.onDidAddNotification(newNotification);
});
}
describe('The JSHint provider for Linter', () => {
const { lint } = linter.provideLinter();
beforeEach(async () => {
await atom.packages.activatePackage('linter-jshint');
await atom.packages.activatePackage('language-javascript');
});
it('should be in the packages list', () => {
expect(atom.packages.isPackageLoaded('linter-jshint')).toBe(true);
});
it('should be an active package', () => {
expect(atom.packages.isPackageActive('linter-jshint')).toBe(true);
});
describe('shows errors in a file with issues', () => {
let editor = null;
beforeEach(async () => {
editor = await atom.workspace.open(bitwisePath);
});
it('verifies the first message', async () => {
const expected = "W016 - Unexpected use of '&'.";
const messages = await lint(editor);
expect(messages[0].severity).toBe('warning');
expect(messages[0].excerpt).toBe(expected);
expect(messages[0].location.file).toBe(bitwisePath);
expect(messages[0].location.position).toEqual([[0, 10], [0, 13]]);
});
});
it('finds nothing wrong with an empty file', async () => {
const emptyPath = path.join(__dirname, 'fixtures', 'empty.js');
const editor = await atom.workspace.open(emptyPath);
const messages = await lint(editor);
expect(messages.length).toBe(0);
});
it('finds nothing wrong with a valid file', async () => {
const editor = await atom.workspace.open(goodPath);
const messages = await lint(editor);
expect(messages.length).toBe(0);
});
describe('shows syntax errors', () => {
const syntaxPath = path.join(__dirname, 'fixtures', 'syntax', 'badSyntax.js');
let editor = null;
beforeEach(async () => {
editor = await atom.workspace.open(syntaxPath);
});
it('verifies the first message', async () => {
const message = 'E006 - Unexpected early end of program.';
const messages = await lint(editor);
expect(messages[0].severity).toBe('error');
expect(messages[0].excerpt).toBe(message);
expect(messages[0].location.file).toBe(syntaxPath);
expect(messages[0].location.position).toEqual([[0, 10], [0, 11]]);
});
});
describe('handles .jshintignore files', () => {
const checkMessage = (message, filePath) => {
const expected = "W098 - 'foo' is defined but never used.";
expect(message.severity).toBe('warning');
expect(message.excerpt).toBe(expected);
expect(message.location.file).toBe(filePath);
expect(message.location.position).toEqual([[0, 4], [0, 7]]);
};
it('works when in the same directory', async () => {
const ignoreDir = path.join(__dirname, 'fixtures', 'ignore');
const checkedPath = path.join(ignoreDir, 'checked.js');
const ignoredPath = path.join(ignoreDir, 'ignored.js');
const checkEditor = await atom.workspace.open(checkedPath);
const ignoreEditor = await atom.workspace.open(ignoredPath);
const checkMessages = await lint(checkEditor);
const ignoreMessages = await lint(ignoreEditor);
expect(checkMessages.length).toBe(1);
checkMessage(checkMessages[0], checkedPath);
expect(ignoreMessages.length).toBe(0);
});
it('handles relative paths in .jshintignore', async () => {
const ignoreDir = path.join(__dirname, 'fixtures', 'ignore-relative', 'js');
const checkedPath = path.join(ignoreDir, 'checked.js');
const ignoredPath = path.join(ignoreDir, 'ignored.js');
const checkEditor = await atom.workspace.open(checkedPath);
const ignoreEditor = await atom.workspace.open(ignoredPath);
const checkMessages = await lint(checkEditor);
const ignoreMessages = await lint(ignoreEditor);
expect(checkMessages.length).toBe(1);
checkMessage(checkMessages[0], checkedPath);
expect(ignoreMessages.length).toBe(0);
});
});
describe('prints debugging information with the `debug` command', () => {
let editor;
const expectedMessage = 'linter-jshint:: Debugging information';
beforeEach(async () => {
editor = await atom.workspace.open(goodPath);
});
it('shows an info notification', async () => {
atom.commands.dispatch(atom.views.getView(editor), 'linter-jshint:debug');
const notification = await getNotification(expectedMessage);
expect(notification.getMessage()).toBe(expectedMessage);
expect(notification.getType()).toEqual('info');
});
it('includes debugging information in the details', async () => {
atom.commands.dispatch(atom.views.getView(editor), 'linter-jshint:debug');
const notification = await getNotification(expectedMessage);
const detail = notification.getDetail();
expect(detail.includes(`Atom version: ${atom.getVersion()}`)).toBe(true);
expect(detail.includes('linter-jshint version:')).toBe(true);
expect(detail.includes(`Platform: ${process.platform}`)).toBe(true);
expect(detail.includes('linter-jshint configuration:')).toBe(true);
});
});
});