Skip to content

Commit

Permalink
added store.enterWith()
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed May 21, 2024
1 parent 3f718ed commit f8a841a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ export default function doWork () {
}
```

## `.enterWith()`

In case `.runInAsyncScope()` is incovenient, you can use `.enterWith()`

```js
import fastify from 'fastify'
import asyncforge from 'fastify-asyncforge'

const fastify = Fastify()

// Calling .enterWith() is necessary or `asyncforge.app()` will throw
fastify.enterWith()
asyncforge.app()
```

### Notice

Note that you cannot wrap `.enterWith()` inside an async function, as it will not work.
If you are interested in knowing more, read https://github.com/nodejs/node/issues/53037.

## License

MIT
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const fastifyAsyncForge = fp(function (fastify, opts, next) {
})
})

fastify.decorate('enterWith', function () {
const store = create()
store.enterWith()
app.set(this)
logger.set(this.log)
})

fastify.addHook('onRequest', function (req, res, next) {
const store = create()

Expand Down
41 changes: 41 additions & 0 deletions test/before.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const { describe, it, before } = require('node:test')
const assert = require('node:assert')
const Fastify = require('fastify')
const asyncforge = require('../')

describe('support exiting from a context', () => {
let fastify
before(async () => {
fastify = Fastify()
await fastify.register(asyncforge)
})

it('throws', () => {
assert.throws(asyncforge.app)
})

it('does not throw using enterWith', () => {
fastify.enterWith()

Check failure on line 20 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

does not throw using enterWith

[Error [ERR_TEST_FAILURE]: store.enterWith is not a function] { failureType: 'testCodeFailure', cause: TypeError [Error]: store.enterWith is not a function at Object.enterWith (/home/runner/work/fastify-asyncforge/fastify-asyncforge/index.js:23:11) at TestContext.<anonymous> (/home/runner/work/fastify-asyncforge/fastify-asyncforge/test/before.test.js:20:13) at Test.runInAsyncScope (node:async_hooks:203:9) at Test.run (node:internal/test_runner/test:631:25) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7), code: 'ERR_TEST_FAILURE' }

Check failure on line 20 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

does not throw using enterWith

[Error [ERR_TEST_FAILURE]: store.enterWith is not a function] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: store.enterWith is not a function at Object.enterWith (/home/runner/work/fastify-asyncforge/fastify-asyncforge/index.js:23:11) at TestContext.<anonymous> (/home/runner/work/fastify-asyncforge/fastify-asyncforge/test/before.test.js:20:13) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:656:25) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Suite.processPendingSubtests (node:internal/test_runner/test:389:7) }

Check failure on line 20 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

does not throw using enterWith

[Error [ERR_TEST_FAILURE]: store.enterWith is not a function] { failureType: 'testCodeFailure', cause: TypeError [Error]: store.enterWith is not a function at Object.enterWith (/home/runner/work/fastify-asyncforge/fastify-asyncforge/index.js:23:11) at TestContext.<anonymous> (/home/runner/work/fastify-asyncforge/fastify-asyncforge/test/before.test.js:20:13) at Test.runInAsyncScope (node:async_hooks:203:9) at Test.run (node:internal/test_runner/test:631:25) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Suite.processPendingSubtests (node:internal/test_runner/test:374:7), code: 'ERR_TEST_FAILURE' }

Check failure on line 20 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

does not throw using enterWith

[Error [ERR_TEST_FAILURE]: store.enterWith is not a function] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: store.enterWith is not a function at Object.enterWith (/home/runner/work/fastify-asyncforge/fastify-asyncforge/index.js:23:11) at TestContext.<anonymous> (/home/runner/work/fastify-asyncforge/fastify-asyncforge/test/before.test.js:20:13) at Test.runInAsyncScope (node:async_hooks:206:9) at Test.run (node:internal/test_runner/test:656:25) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Suite.processPendingSubtests (node:internal/test_runner/test:389:7) }
assert.equal(asyncforge.app(), fastify)
})
})

describe('enterWith in before', () => {
let fastify
before(async () => {
fastify = Fastify()
await fastify.register(asyncforge)
fastify.enterWith()

Check failure on line 30 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

enterWith in before

[Error [ERR_TEST_FAILURE]: failed running before hook] { failureType: 'hookFailed', cause: TypeError [Error]: store.enterWith is not a function at Object.enterWith (/home/runner/work/fastify-asyncforge/fastify-asyncforge/index.js:23:11) at SuiteContext.<anonymous> (/home/runner/work/fastify-asyncforge/fastify-asyncforge/test/before.test.js:30:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestHook.run (node:internal/test_runner/test:632:9) at async node:internal/test_runner/test:565:9 at async Suite.runHook (node:internal/test_runner/test:563:7) at async Suite.run (node:internal/test_runner/test:942:7) at async Test.processPendingSubtests (node:internal/test_runner/test:374:7), code: 'ERR_TEST_FAILURE' }

Check failure on line 30 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

enterWith in before

[Error [ERR_TEST_FAILURE]: failed running before hook] { code: 'ERR_TEST_FAILURE', failureType: 'hookFailed', cause: TypeError [Error]: store.enterWith is not a function at Object.enterWith (/home/runner/work/fastify-asyncforge/fastify-asyncforge/index.js:23:11) at SuiteContext.<anonymous> (/home/runner/work/fastify-asyncforge/fastify-asyncforge/test/before.test.js:30:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestHook.run (node:internal/test_runner/test:657:9) at async node:internal/test_runner/test:588:9 at async Suite.runHook (node:internal/test_runner/test:586:7) at async Suite.run (node:internal/test_runner/test:983:7) at async Test.processPendingSubtests (node:internal/test_runner/test:389:7) }

Check failure on line 30 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

enterWith in before

[Error [ERR_TEST_FAILURE]: failed running before hook] { failureType: 'hookFailed', cause: TypeError [Error]: store.enterWith is not a function at Object.enterWith (/home/runner/work/fastify-asyncforge/fastify-asyncforge/index.js:23:11) at SuiteContext.<anonymous> (/home/runner/work/fastify-asyncforge/fastify-asyncforge/test/before.test.js:30:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestHook.run (node:internal/test_runner/test:632:9) at async node:internal/test_runner/test:565:9 at async Suite.runHook (node:internal/test_runner/test:563:7) at async Suite.run (node:internal/test_runner/test:942:7) at async Test.processPendingSubtests (node:internal/test_runner/test:374:7), code: 'ERR_TEST_FAILURE' }

Check failure on line 30 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

enterWith in before

[Error [ERR_TEST_FAILURE]: failed running before hook] { code: 'ERR_TEST_FAILURE', failureType: 'hookFailed', cause: TypeError [Error]: store.enterWith is not a function at Object.enterWith (/home/runner/work/fastify-asyncforge/fastify-asyncforge/index.js:23:11) at SuiteContext.<anonymous> (/home/runner/work/fastify-asyncforge/fastify-asyncforge/test/before.test.js:30:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestHook.run (node:internal/test_runner/test:657:9) at async node:internal/test_runner/test:588:9 at async Suite.runHook (node:internal/test_runner/test:586:7) at async Suite.run (node:internal/test_runner/test:983:7) at async Test.processPendingSubtests (node:internal/test_runner/test:389:7) }
})

it('throws', () => {

Check failure on line 33 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

throws

[Error [ERR_TEST_FAILURE]: test did not finish before its parent and was cancelled] { failureType: 'cancelledByParent', cause: 'test did not finish before its parent and was cancelled', code: 'ERR_TEST_FAILURE' }

Check failure on line 33 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

throws

[Error [ERR_TEST_FAILURE]: test did not finish before its parent and was cancelled] { code: 'ERR_TEST_FAILURE', failureType: 'cancelledByParent', cause: 'test did not finish before its parent and was cancelled' }

Check failure on line 33 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

throws

[Error [ERR_TEST_FAILURE]: test did not finish before its parent and was cancelled] { failureType: 'cancelledByParent', cause: 'test did not finish before its parent and was cancelled', code: 'ERR_TEST_FAILURE' }

Check failure on line 33 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

throws

[Error [ERR_TEST_FAILURE]: test did not finish before its parent and was cancelled] { code: 'ERR_TEST_FAILURE', failureType: 'cancelledByParent', cause: 'test did not finish before its parent and was cancelled' }
assert.throws(asyncforge.app)
})

it('does not throw using enterWith again', () => {

Check failure on line 37 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

does not throw using enterWith again

[Error [ERR_TEST_FAILURE]: test did not finish before its parent and was cancelled] { failureType: 'cancelledByParent', cause: 'test did not finish before its parent and was cancelled', code: 'ERR_TEST_FAILURE' }

Check failure on line 37 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

does not throw using enterWith again

[Error [ERR_TEST_FAILURE]: test did not finish before its parent and was cancelled] { code: 'ERR_TEST_FAILURE', failureType: 'cancelledByParent', cause: 'test did not finish before its parent and was cancelled' }

Check failure on line 37 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (18.x)

does not throw using enterWith again

[Error [ERR_TEST_FAILURE]: test did not finish before its parent and was cancelled] { failureType: 'cancelledByParent', cause: 'test did not finish before its parent and was cancelled', code: 'ERR_TEST_FAILURE' }

Check failure on line 37 in test/before.test.js

View workflow job for this annotation

GitHub Actions / test (20.x)

does not throw using enterWith again

[Error [ERR_TEST_FAILURE]: test did not finish before its parent and was cancelled] { code: 'ERR_TEST_FAILURE', failureType: 'cancelledByParent', cause: 'test did not finish before its parent and was cancelled' }
fastify.enterWith()
assert.equal(asyncforge.app(), fastify)
})
})

0 comments on commit f8a841a

Please sign in to comment.