Skip to content

Commit 8c1be5c

Browse files
committed
test(api/format): add some coverage
1 parent 2dbe32d commit 8c1be5c

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/api/__mocks__/prettier.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
format: jest.fn(() => '[formatted-code]'),
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`format calls \`prettier.format\` with internal Prettier configuration 1`] = `
4+
Object {
5+
"arrowParens": "avoid",
6+
"bracketSpacing": true,
7+
"endOfLine": "lf",
8+
"htmlWhitespaceSensitivity": "css",
9+
"insertPragma": false,
10+
"jsxBracketSameLine": false,
11+
"jsxSingleQuote": false,
12+
"printWidth": 80,
13+
"proseWrap": "always",
14+
"quoteProps": "as-needed",
15+
"requirePragma": false,
16+
"semi": true,
17+
"singleQuote": true,
18+
"tabWidth": 2,
19+
"trailingComma": "all",
20+
"useTabs": false,
21+
}
22+
`;
23+
24+
exports[`format merges provided \`options\` with internal Prettier configuration 1`] = `
25+
Object {
26+
"arrowParens": "always",
27+
"bracketSpacing": true,
28+
"endOfLine": "lf",
29+
"htmlWhitespaceSensitivity": "css",
30+
"insertPragma": false,
31+
"jsxBracketSameLine": false,
32+
"jsxSingleQuote": false,
33+
"parser": "css",
34+
"printWidth": 100,
35+
"proseWrap": "always",
36+
"quoteProps": "as-needed",
37+
"requirePragma": false,
38+
"semi": true,
39+
"singleQuote": true,
40+
"tabWidth": 2,
41+
"trailingComma": "all",
42+
"useTabs": false,
43+
}
44+
`;

src/api/__tests__/api.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import cases from 'jest-in-case'
2+
import prettier from 'prettier'
3+
import {format} from '../'
4+
5+
cases(
6+
'format',
7+
({source = 'const id = x => x', options = {}}) => {
8+
// beforeEach
9+
format(source, options)
10+
11+
try {
12+
expect(prettier.format).toHaveBeenCalledTimes(1)
13+
expect(prettier.format).toHaveBeenCalledWith(source, expect.any(Object))
14+
15+
const [call] = prettier.format.mock.calls
16+
17+
expect(call[1]).toMatchSnapshot()
18+
} catch (error) {
19+
throw error
20+
} finally {
21+
// afterEach
22+
23+
prettier.format.mockReset()
24+
jest.resetModules()
25+
}
26+
},
27+
{
28+
'calls `prettier.format` with internal Prettier configuration': {},
29+
'merges provided `options` with internal Prettier configuration': {
30+
/** @type {import('prettier').Options} */
31+
options: {printWidth: 100, arrowParens: 'always', parser: 'css'},
32+
},
33+
},
34+
)

src/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "./config/tsconfig.json",
33
"include": ["./api"],
4+
"exclude": ["./**/__tests__/*"],
45
"compilerOptions": {
56
"allowJs": true,
67
"checkJs": true,

0 commit comments

Comments
 (0)