Skip to content

Commit 1018801

Browse files
authored
[0.1.x] Support Inertia clearErrors with arguments (#7)
* Migrate to vitest * Support clearing specific errors via Inertia clearErrors * lint * build packages before testing * Remove dedicated build workflow
1 parent bf8ba2d commit 1018801

File tree

14 files changed

+145
-117
lines changed

14 files changed

+145
-117
lines changed

.eslintrc.cjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module.exports = {
22
root: true,
33
env: {
44
browser: true,
5-
jest: true,
65
node: true,
76
},
87
parser: "@typescript-eslint/parser",

.github/workflows/build.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ jobs:
2525
- name: Install dependencies
2626
run: npm install
2727

28+
- name: Build packages
29+
run: npm run build
30+
2831
- name: Execute tests
2932
run: npm run test

packages/core/babel.config.cjs

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/core/jest.config.cjs

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/core/package.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
],
2222
"scripts": {
2323
"build": "rm -rf dist && tsc",
24-
"test": "jest",
25-
"prepublishOnly": "npm run build"
24+
"prepublishOnly": "npm run build",
25+
"test": "vitest run"
2626
},
2727
"engines": {
2828
"node": ">=14"
@@ -37,19 +37,14 @@
3737
"lodash.set": "^4.3.2"
3838
},
3939
"devDependencies": {
40-
"@babel/core": "^7.18.13",
41-
"@babel/preset-env": "^7.18.10",
42-
"@babel/preset-typescript": "^7.18.6",
43-
"@types/jest": "^29.0.0",
4440
"@types/lodash.debounce": "^4.0.7",
4541
"@types/lodash.get": "^4.4.7",
4642
"@types/lodash.isequal": "^4.0.7",
4743
"@types/lodash.merge": "^4.0.7",
4844
"@types/lodash.omit": "^4.5.7",
4945
"@types/lodash.set": "^4.3.7",
5046
"@types/node": "^20.1.0",
51-
"babel-jest": "^29.5.0",
52-
"ts-jest": "^29.0.0",
53-
"typescript": "^5.0.0"
47+
"typescript": "^5.0.0",
48+
"vitest": "^0.31.3"
5449
}
5550
}
Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
import { it, vi, expect, beforeEach, afterEach } from 'vitest'
12
import axios from 'axios'
23
import { client } from '../src/index'
34
import { createValidator } from '../src/validator'
45

5-
jest.mock('axios')
6-
client.use(axios)
7-
jest.useFakeTimers()
6+
beforeEach(() => {
7+
vi.mock('axios')
8+
vi.useFakeTimers()
9+
})
10+
11+
afterEach(() => {
12+
vi.restoreAllMocks()
13+
vi.runAllTimers()
14+
})
815

9-
test('it can handle a successful precognition response via config handler', async () => {
16+
it('can handle a successful precognition response via config handler', async () => {
1017
expect.assertions(2)
1118

1219
const response = { headers: { precognition: 'true' }, status: 204, data: 'data' }
@@ -21,7 +28,7 @@ test('it can handle a successful precognition response via config handler', asyn
2128
}).then(value => expect(value).toBe('expected value'))
2229
})
2330

24-
test('it can handle a success response via a fulfilled promise', async () => {
31+
it('can handle a success response via a fulfilled promise', async () => {
2532
expect.assertions(1)
2633

2734
const response = { headers: { precognition: 'true' }, status: 204, data: 'data' }
@@ -30,7 +37,7 @@ test('it can handle a success response via a fulfilled promise', async () => {
3037
await client.post('https://laravel.com').then(r => expect(r).toBe(response))
3138
})
3239

33-
test('it can handle a validation response via a config handler', async () => {
40+
it('can handle a validation response via a config handler', async () => {
3441
expect.assertions(3)
3542

3643
const error = {
@@ -56,7 +63,7 @@ test('it can handle a validation response via a config handler', async () => {
5663
}).then(value => expect(value).toBe('expected value'))
5764
})
5865

59-
test('it can handle an unauthorized response via a config handler', async () => {
66+
it('can handle an unauthorized response via a config handler', async () => {
6067
expect.assertions(3)
6168

6269
const error = {
@@ -79,7 +86,7 @@ test('it can handle an unauthorized response via a config handler', async () =>
7986
}).then(value => expect(value).toBe('expected value'))
8087
})
8188

82-
test('it can handle a forbidden response via a config handler', async () => {
89+
it('can handle a forbidden response via a config handler', async () => {
8390
expect.assertions(3)
8491

8592
const error = {
@@ -102,7 +109,7 @@ test('it can handle a forbidden response via a config handler', async () => {
102109
}).then(value => expect(value).toBe('expected value'))
103110
})
104111

105-
test('it can handle a not found response via a config handler', async () => {
112+
it('can handle a not found response via a config handler', async () => {
106113
expect.assertions(3)
107114

108115
const error = {
@@ -125,7 +132,7 @@ test('it can handle a not found response via a config handler', async () => {
125132
}).then(value => expect(value).toBe('expected value'))
126133
})
127134

128-
test('it can handle a conflict response via a config handler', async () => {
135+
it('can handle a conflict response via a config handler', async () => {
129136
expect.assertions(3)
130137

131138
const error = {
@@ -148,7 +155,7 @@ test('it can handle a conflict response via a config handler', async () => {
148155
}).then(value => expect(value).toBe('expected value'))
149156
})
150157

151-
test('it can handle a locked response via a config handler', async () => {
158+
it('can handle a locked response via a config handler', async () => {
152159
expect.assertions(3)
153160

154161
const error = {
@@ -171,7 +178,7 @@ test('it can handle a locked response via a config handler', async () => {
171178
}).then(value => expect(value).toBe('expected value'))
172179
})
173180

174-
test('it can provide input names to validate via config', async () => {
181+
it('can provide input names to validate via config', async () => {
175182
expect.assertions(1)
176183

177184
let config
@@ -187,7 +194,7 @@ test('it can provide input names to validate via config', async () => {
187194
expect(config.headers['Precognition-Validate-Only']).toBe('username,email')
188195
})
189196

190-
test('it throws an error if the precognition header is not present on a success response', async () => {
197+
it('throws an error if the precognition header is not present on a success response', async () => {
191198
expect.assertions(2)
192199

193200
axios.request.mockResolvedValueOnce({ headers: { status: 204 } })
@@ -198,7 +205,7 @@ test('it throws an error if the precognition header is not present on a success
198205
})
199206
})
200207

201-
test('it throws an error if the precognition header is not present on an error response', async () => {
208+
it('throws an error if the precognition header is not present on an error response', async () => {
202209
expect.assertions(2)
203210

204211
axios.request.mockRejectedValueOnce({ response: { status: 500 } })
@@ -210,7 +217,7 @@ test('it throws an error if the precognition header is not present on an error r
210217
})
211218
})
212219

213-
test('it returns a non-axios error via a rejected promise', async () => {
220+
it('returns a non-axios error via a rejected promise', async () => {
214221
expect.assertions(1)
215222

216223
const error = { expected: 'error' }
@@ -222,7 +229,7 @@ test('it returns a non-axios error via a rejected promise', async () => {
222229
})
223230
})
224231

225-
test('returns a canceled request error via a rejected promise', async () => {
232+
it('returns a canceled request error va rejected promise', async () => {
226233
expect.assertions(1)
227234

228235
const error = { expected: 'error' }
@@ -235,7 +242,7 @@ test('returns a canceled request error via a rejected promise', async () => {
235242
})
236243
})
237244

238-
test('an axios error without a "status" property returns a rejected promise', async () => {
245+
it('an axerror without a "status" property returns a rejected promise', async () => {
239246
expect.assertions(1)
240247

241248
const error = { expected: 'error' }
@@ -247,7 +254,7 @@ test('an axios error without a "status" property returns a rejected promise', as
247254
})
248255
})
249256

250-
test('it can handle error responses via a rejected promise', async () => {
257+
it('can handle error responses via a rejected promise', async () => {
251258
expect.assertions(1)
252259

253260
const error = {
@@ -266,7 +273,7 @@ test('it can handle error responses via a rejected promise', async () => {
266273
await client.get('https://laravel.com').catch(e => expect(e).toBe(error))
267274
})
268275

269-
test('it can customize how it determines a successful precognition response', async () => {
276+
it('can customize how it determines a successful precognition response', async () => {
270277
expect.assertions(3)
271278

272279
let response = { headers: { precognition: 'true' }, status: 999, data: 'data' }
@@ -292,7 +299,7 @@ test('it can customize how it determines a successful precognition response', as
292299
}).then(value => expect(value).toBe(response))
293300
})
294301

295-
test('it creates a request fingerprint and an abort signal if none are configured', async () => {
302+
it('creates a request fingerprint and an abort signal if none are configured', async () => {
296303
expect.assertions(2)
297304

298305
let config
@@ -307,7 +314,7 @@ test('it creates a request fingerprint and an abort signal if none are configure
307314
expect(config.signal).toBeInstanceOf(AbortSignal)
308315
})
309316

310-
test('it uses the default axios baseURL in the request fingerprint', async () => {
317+
it('uses the default axios baseURL in the request fingerprint', async () => {
311318
expect.assertions(2)
312319

313320
let config
@@ -323,7 +330,7 @@ test('it uses the default axios baseURL in the request fingerprint', async () =>
323330
expect(config.signal).toBeInstanceOf(AbortSignal)
324331
})
325332

326-
test('the configured baseURL takes precedence over the axios default baseURL for request id', async () => {
333+
it('the confbaseURL takes precedence over the axios default baseURL for request id', async () => {
327334
expect.assertions(2)
328335

329336
let config
@@ -341,7 +348,7 @@ test('the configured baseURL takes precedence over the axios default baseURL for
341348
expect(config.signal).toBeInstanceOf(AbortSignal)
342349
})
343350

344-
test('it can specify the request fingerprint via config', async () => {
351+
it('can specify the request fingerprint via config', async () => {
345352
expect.assertions(2)
346353

347354
let config
@@ -358,7 +365,7 @@ test('it can specify the request fingerprint via config', async () => {
358365
expect(config.signal).toBeInstanceOf(AbortSignal)
359366
})
360367

361-
test('it can customize how the request fingerprint is created', async () => {
368+
it('can customize how the request fingerprint is created', async () => {
362369
expect.assertions(2)
363370

364371
let config
@@ -374,7 +381,7 @@ test('it can customize how the request fingerprint is created', async () => {
374381
expect(config.signal).toBeInstanceOf(AbortSignal)
375382
})
376383

377-
test('the config fingerprint takes precedence over the global fingerprint for request id', async () => {
384+
it('the conffingerprint takes precedence over the global fingerprint for request id', async () => {
378385
expect.assertions(2)
379386

380387
let config
@@ -392,7 +399,7 @@ test('the config fingerprint takes precedence over the global fingerprint for re
392399
expect(config.signal).toBeInstanceOf(AbortSignal)
393400
})
394401

395-
test('it can opt out of automatic request aborting', async () => {
402+
it('can opt out of automatic request aborting', async () => {
396403
expect.assertions(2)
397404

398405
let config
@@ -409,7 +416,7 @@ test('it can opt out of automatic request aborting', async () => {
409416
expect(config.signal).toBeUndefined()
410417
})
411418

412-
test('it can specify the abort controller via config', async () => {
419+
it('can specify the abort controller via config', async () => {
413420
expect.assertions(1)
414421

415422
let config
@@ -431,7 +438,7 @@ test('it can specify the abort controller via config', async () => {
431438
expect(called).toBe(true)
432439
})
433440

434-
test('it does not create an abort controller when a cancelToken is provided', async () => {
441+
it('does not create an abort controller when a cancelToken is provided', async () => {
435442
expect.assertions(1)
436443

437444
let config
@@ -447,7 +454,7 @@ test('it does not create an abort controller when a cancelToken is provided', as
447454
expect(config.signal).toBeUndefined()
448455
})
449456

450-
test('revalidates data when validate is called', async () => {
457+
it('revaldata when validate is called', async () => {
451458
expect.assertions(4)
452459

453460
let requests = 0
@@ -464,20 +471,20 @@ test('revalidates data when validate is called', async () => {
464471
data = { name: 'Tim' }
465472
validator.validate('name', 'Tim')
466473
expect(requests).toBe(1)
467-
jest.advanceTimersByTime(1500)
474+
vi.advanceTimersByTime(1500)
468475

469476
data = { name: 'Jess' }
470477
validator.validate('name', 'Jess')
471478
expect(requests).toBe(2)
472-
jest.advanceTimersByTime(1500)
479+
vi.advanceTimersByTime(1500)
473480

474481
data = { name: 'Taylor' }
475482
validator.validate('name', 'Taylor')
476483
expect(requests).toBe(3)
477-
jest.advanceTimersByTime(1500)
484+
vi.advanceTimersByTime(1500)
478485
})
479486

480-
test('does not revalidate data when data is unchanged', async () => {
487+
it('does not revaldata when data is unchanged', async () => {
481488
expect.assertions(4)
482489

483490
let requests = 0
@@ -494,15 +501,15 @@ test('does not revalidate data when data is unchanged', async () => {
494501
data = { first: true }
495502
validator.validate('name', true)
496503
expect(requests).toBe(1)
497-
jest.advanceTimersByTime(1500)
504+
vi.advanceTimersByTime(1500)
498505

499506
data = { first: true }
500507
validator.validate('name', true)
501508
expect(requests).toBe(1)
502-
jest.advanceTimersByTime(1500)
509+
vi.advanceTimersByTime(1500)
503510

504511
data = { second: true }
505512
validator.validate('name', true)
506513
expect(requests).toBe(2)
507-
jest.advanceTimersByTime(1500)
514+
vi.advanceTimersByTime(1500)
508515
})

0 commit comments

Comments
 (0)