Skip to content

Commit

Permalink
chore: use eslint-plugin-jest to lint test code (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
mure authored Sep 26, 2023
1 parent 10645ec commit ccf66dc
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 32 deletions.
9 changes: 8 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"extends": "./.config/.eslintrc"
"extends": "./.config/.eslintrc",
"overrides": [
{
"files": ["**/*.test.ts", "**/*.test.tsx"],
"plugins": ["jest"],
"extends": ["plugin:jest/recommended"]
}
]
}
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^6.7.3",
"eslint": "^8.43.0",
"eslint-plugin-jest": "^27.4.0",
"eslint-webpack-plugin": "^4.0.1",
"fork-ts-checker-webpack-plugin": "^8.0.0",
"glob": "^10.2.7",
Expand Down
22 changes: 11 additions & 11 deletions src/datasources/data-frame/DataFrameDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ it('should return data ignoring invalid queries', async () => {

await ds.query(query);

expect(fetchMock).toBeCalledTimes(2);
expect(fetchMock).toBeCalledWith(expect.objectContaining({ url: '_/nidataframe/v1/tables/1/query-decimated-data' }));
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(fetchMock).toHaveBeenCalledWith(expect.objectContaining({ url: '_/nidataframe/v1/tables/1/query-decimated-data' }));
});

it('should return data for multiple targets', async () => {
Expand All @@ -57,7 +57,7 @@ it('should return data for multiple targets', async () => {

const response = await ds.query(query);

expect(fetchMock).toBeCalledTimes(4);
expect(fetchMock).toHaveBeenCalledTimes(4);
expect(response.data).toHaveLength(2);
});

Expand Down Expand Up @@ -99,7 +99,7 @@ it('should automatically apply time filters when index column is a timestamp', a

await ds.query(query);

expect(fetchMock).toBeCalledWith(
expect(fetchMock).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
filters: [
Expand All @@ -123,7 +123,7 @@ it('should apply null and NaN filters', async () => {

await ds.query(query);

expect(fetchMock).toBeCalledWith(
expect(fetchMock).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
filters: [
Expand All @@ -149,7 +149,7 @@ it('should provide decimation parameters correctly', async () => {

await ds.query(query);

expect(fetchMock).toBeCalledWith(
expect(fetchMock).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
decimation: { intervals: 300, method: 'ENTRY_EXIT', yColumns: ['int', 'float'] },
Expand All @@ -163,12 +163,12 @@ it('should cache table metadata for subsequent requests', async () => {

await ds.query(query);

expect(fetchMock).toBeCalledTimes(2);
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(fetchMock).toHaveBeenCalledWith(expect.objectContaining({ url: '_/nidataframe/v1/tables/1' }));

await ds.query(query);

expect(fetchMock).toBeCalledTimes(3);
expect(fetchMock).toHaveBeenCalledTimes(3);
});

it('should return error if query columns do not match table metadata', async () => {
Expand All @@ -188,7 +188,7 @@ it('should migrate queries using columns of arrays of objects', async () => {

await ds.query(query);

expect(fetchMock).toBeCalledWith(expect.objectContaining({ data: expect.objectContaining({ columns: ['float'] }) }));
expect(fetchMock).toHaveBeenCalledWith(expect.objectContaining({ data: expect.objectContaining({ columns: ['float'] }) }));
});

it('attempts to replace variables in metadata query', async () => {
Expand All @@ -197,7 +197,7 @@ it('attempts to replace variables in metadata query', async () => {

await ds.getTableMetadata(tableId);

expect(replaceMock).toBeCalledTimes(1);
expect(replaceMock).toHaveBeenCalledTimes(1);
expect(replaceMock).toHaveBeenCalledWith(tableId);
});

Expand All @@ -207,7 +207,7 @@ it('attempts to replace variables in data query', async () => {

await ds.query(query);

expect(replaceMock).toBeCalledTimes(2);
expect(replaceMock).toHaveBeenCalledTimes(2);
expect(replaceMock).toHaveBeenCalledWith(query.targets[0].tableId, expect.anything());
});

Expand Down
12 changes: 6 additions & 6 deletions src/datasources/notebook/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ describe('Notebook data source', () => {

ds.replaceParameterVariables(parameters, options);

expect(replaceMock).toBeCalledTimes(2);
expect(replaceMock).toBeCalledWith(s1, expect.anything());
expect(replaceMock).toBeCalledWith(s2, expect.anything());
expect(replaceMock).toHaveBeenCalledTimes(2);
expect(replaceMock).toHaveBeenCalledWith(s1, expect.anything());
expect(replaceMock).toHaveBeenCalledWith(s2, expect.anything());
});

it('does not attempt to replace variables in non-string parameters', () => {
Expand All @@ -213,7 +213,7 @@ describe('Notebook data source', () => {

ds.replaceParameterVariables(parameters, options);

expect(replaceMock).not.toBeCalled();
expect(replaceMock).not.toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -291,7 +291,7 @@ describe('Notebook data source', () => {
],
} as unknown as DataQueryRequest<NotebookQuery>;

expect(ds.query(options)).rejects.toThrow();
await expect(ds.query(options)).rejects.toThrow();
});

it('throws error for notebook execution with invalid output', async () => {
Expand All @@ -306,7 +306,7 @@ describe('Notebook data source', () => {
],
} as unknown as DataQueryRequest<NotebookQuery>;

expect(ds.query(options)).rejects.toThrow();
await expect(ds.query(options)).rejects.toThrow();
});

it('executes notebook with resultCachePeriod', async () => {
Expand Down
14 changes: 6 additions & 8 deletions src/datasources/system/SystemDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,11 @@ test('query metadata for one system', async () => {

test('query metadata with templated system name', async () => {
templateSrv.replace.calledWith('$system_id').mockReturnValue('system-1');
backendSrv.fetch
.calledWith(
requestMatching({ url: '/nisysmgmt/v1/query-systems', data: { filter: 'id = "system-1" || alias = "system-1"' } })
)
.mockReturnValue(createFetchResponse({ data: [fakeSystems[0]] }));
backendSrv.fetch.mockReturnValue(createFetchResponse({ data: [fakeSystems[0]] }));

await ds.query(buildQuery({ queryKind: SystemQueryType.Metadata, systemName: '$system_id' }));

expect(backendSrv.fetch.mock.lastCall?.[0].data).toHaveProperty('filter', 'id = "system-1" || alias = "system-1"');
});

test('queries for system variable values - all workspaces', async () => {
Expand All @@ -109,11 +107,11 @@ test('queries for system variable values - all workspaces', async () => {
});

test('queries for system variable values - single workspace', async () => {
backendSrv.fetch
.calledWith(requestMatching({ url: '/nisysmgmt/v1/query-systems', data: { filter: 'workspace = "1"' } }))
.mockReturnValue(createFetchResponse({ data: fakeSystems.map(({ id, alias }) => ({ id, alias })) }));
backendSrv.fetch.mockReturnValue(createFetchResponse({ data: fakeSystems.map(({ id, alias }) => ({ id, alias })) }));

await ds.metricFindQuery({ workspace: '1' });

expect(backendSrv.fetch.mock.lastCall?.[0].data).toHaveProperty('filter', 'workspace = "1"');
});

const fakeSystems: SystemMetadata[] = [
Expand Down
4 changes: 2 additions & 2 deletions src/datasources/system/components/SystemQueryEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ it('updates when user interacts with fields', async () => {

// User changes query type
await userEvent.click(screen.getByRole('radio', { name: 'Metadata' }));
expect(onChange).toBeCalledWith(expect.objectContaining({ queryKind: SystemQueryType.Metadata }));
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ queryKind: SystemQueryType.Metadata }));
expect(screen.getByPlaceholderText('All systems')).toBeInTheDocument();

// User types system name
await userEvent.type(screen.getByLabelText('System'), 'my-system{enter}');
expect(onChange).toBeCalledWith(expect.objectContaining({ systemName: 'my-system' }));
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ systemName: 'my-system' }));
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ test('user selects new workspace', async () => {
await workspacesLoaded();

await select(screen.getByRole('combobox'), 'Other workspace', { container: document.body });
expect(onChange).toBeCalledWith({ workspace: '2' });
expect(onChange).toHaveBeenCalledWith({ workspace: '2' });
});
6 changes: 3 additions & 3 deletions src/datasources/tag/components/TagQueryEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ it('renders with initial query and updates when user makes changes', async () =>

// Users changes query type
await userEvent.click(screen.getByRole('radio', { name: 'Current' }));
expect(onChange).toBeCalledWith(expect.objectContaining({ type: TagQueryType.Current }));
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ type: TagQueryType.Current }));

// User types in new tag path
await userEvent.type(screen.getByLabelText('Tag path'), '.test{enter}');
expect(onChange).toBeCalledWith(expect.objectContaining({ path: 'my.tag.test' }));
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ path: 'my.tag.test' }));

// User selects different workspace
await select(screen.getByRole('combobox'), 'Other workspace', { container: document.body });
expect(onChange).toBeCalledWith(expect.objectContaining({ workspace: '2' }));
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ workspace: '2' }));
});

0 comments on commit ccf66dc

Please sign in to comment.