This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test no-relative-parent-import using RuleTester
- Loading branch information
1 parent
e6f50b5
commit bc28f43
Showing
1 changed file
with
21 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,27 @@ | ||
/* global describe, it, expect, jest, beforeEach */ | ||
/* global */ | ||
|
||
const { RuleTester } = require('eslint'); | ||
const noRelativeParentImport = require('../../src/rules/no-relative-parent-import'); | ||
|
||
describe('no-relative-parent-import', () => { | ||
const context = { | ||
report: jest.fn(), | ||
}; | ||
|
||
const rule = noRelativeParentImport.create(context); | ||
|
||
beforeEach(() => { | ||
context.report.mockClear(); | ||
}); | ||
|
||
it('reports when the import path contains ".."', () => { | ||
const badNode = { | ||
source: { | ||
value: '../relative-parent', | ||
}, | ||
}; | ||
|
||
rule.ImportDeclaration(badNode); | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
expect(context.report).toHaveBeenCalledTimes(1); | ||
expect(context.report).toHaveBeenCalledWith({ | ||
node: badNode, | ||
message: | ||
ruleTester.run('no-relative-parent-import', noRelativeParentImport, { | ||
valid: [ | ||
'import * as React from "react";', | ||
'import { StoreState } from "./store/types";', | ||
'import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'import parent from "../parent";', | ||
errors: [ | ||
'Do not import parent modules with relative "../". Use the "^/" alias.', | ||
}); | ||
}); | ||
|
||
it('does not report when the import path does not contains ".."', () => { | ||
const goodNode = { | ||
source: { | ||
value: '^/absolute', | ||
}, | ||
}; | ||
|
||
rule.ImportDeclaration(goodNode); | ||
|
||
expect(context.report).toHaveBeenCalledTimes(0); | ||
}); | ||
], | ||
}, | ||
], | ||
}); |