Compare commits

..

2 Commits

Author SHA1 Message Date
Kamil Myśliwiec
d7250e42e5 tests() exclude async hooks 2018-12-29 12:45:42 +01:00
Kamil Myśliwiec
6a084a36b6 feature() add async_hooks module (async storage) 2018-12-29 10:59:57 +01:00
1222 changed files with 104403 additions and 348753 deletions

View File

@@ -1,183 +0,0 @@
version: 2
aliases:
- &restore-cache
restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- &install-deps
run:
name: Install dependencies
command: npm ci
- &build-packages
run:
name: Build
command: npm run build
- &run-unit-tests
run:
name: Test
command: npm run test
unit-tests-template: &unit-tests-template
working_directory: ~/nest
steps:
- checkout
- *restore-cache
- *install-deps
- *build-packages
- *run-unit-tests
jobs:
build:
working_directory: ~/nest
docker:
- image: circleci/node:12
steps:
- checkout
- run:
name: Update NPM version
command: 'sudo npm install -g npm@latest'
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: Install dependencies
command: npm ci
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- ./node_modules
- run:
name: Build
command: npm run build
test_node_12:
working_directory: ~/nest
docker:
- image: circleci/node:12
steps:
- checkout
- *restore-cache
- *install-deps
- *build-packages
- *run-unit-tests
- run:
name: Collect coverage
command: npm run coverage
- store_artifacts:
path: coverage
test_node_10:
<<: *unit-tests-template
docker:
- image: circleci/node:10
test_node_8:
<<: *unit-tests-template
docker:
- image: circleci/node:8
lint:
working_directory: ~/nest
docker:
- image: circleci/node:12
steps:
- checkout
- *restore-cache
- *install-deps
- run:
name: Lint
command: npm run lint
- run:
name: Lint commit
command: ./node_modules/.bin/commitlint-circle -c .commitlintrc.json
integration_tests:
working_directory: ~/nest
machine: true
steps:
- checkout
- run:
name: Prepare nvm
command: |
echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
echo ' [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
- run:
name: Upgrade Node.js
command: |
nvm install v12
node -v
nvm alias default v12
- run:
name: Install Docker Compose
command: |
curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
chmod +x ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
- *install-deps
- run:
name: Prepare tests
command: |
bash ./scripts/prepare.sh
sleep 10
- run:
name: List containers
command: docker ps
- run:
name: Integration tests
command: npm run test:integration
codechecks_benchmarks:
working_directory: ~/nest
docker:
- image: circleci/node:12
steps:
- checkout
- *restore-cache
- *install-deps
- *build-packages
- run:
name: Install native wrk
command: .circleci/install-wrk.sh
- run:
name: Run codechecks with benchmarks
command: yarn codechecks:benchmarks
samples:
working_directory: ~/nest
docker:
- image: circleci/node:12
environment:
- DISABLE_OPENCOLLECTIVE: true
steps:
- checkout
- *restore-cache
- *install-deps
- run:
name: Build all samples
command: npm run build:samples
workflows:
version: 2
build-and-test:
jobs:
- build
- test_node_12:
requires:
- build
- test_node_10:
requires:
- build
- test_node_8:
requires:
- build
- lint:
requires:
- build
- integration_tests:
requires:
- build
- samples:
requires:
- build
- codechecks_benchmarks:
requires:
- build

View File

@@ -1,13 +0,0 @@
#!/usr/bin/env bash
set -e
cd "$(dirname "$0")"
# based on https://medium.com/@felipedutratine/intelligent-benchmark-with-wrk-163986c1587f
cd /tmp/
sudo apt-get install build-essential libssl-dev git -y
git clone https://github.com/wg/wrk.git wrk
cd wrk
sudo make
# move the executable to somewhere in your PATH, ex:
sudo cp wrk /usr/local/bin

View File

@@ -1,28 +0,0 @@
{
"extends": ["@commitlint/config-angular"],
"rules": {
"subject-case": [
2,
"always",
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case"]
],
"type-enum": [
2,
"always",
[
"build",
"chore",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test",
"sample"
]
]
}
}

View File

@@ -1 +0,0 @@
**/node_modules/**

View File

@@ -1,24 +0,0 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
root: true,
env: {
node: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
},
};

View File

@@ -1,13 +0,0 @@
const baseConfig = require('./.eslintrc');
module.exports = {
...baseConfig,
parserOptions: {
project: 'tsconfig.spec.json',
sourceType: 'module',
},
rules: {
...baseConfig.rules,
'@typescript-eslint/no-empty-function': 'off',
},
};

4
.github/FUNDING.yml vendored
View File

@@ -1,4 +0,0 @@
# These are supported funding model platforms
github: [kamilmysliwiec]
open_collective: nest

46
.github/ISSUE_TEMPLATE.md vendored Normal file
View File

@@ -0,0 +1,46 @@
<!--
PLEASE HELP US PROCESS GITHUB ISSUES FASTER BY PROVIDING THE FOLLOWING INFORMATION.
ISSUES MISSING IMPORTANT INFORMATION MAY BE CLOSED WITHOUT INVESTIGATION.
-->
## I'm submitting a...
<!--
Please search GitHub for a similar issue or PR before submitting.
Check one of the following options with "x" -->
<pre><code>
[ ] Regression <!--(a behavior that used to work and stopped working in a new release)-->
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
</code></pre>
## Current behavior
<!-- Describe how the issue manifests. -->
## Expected behavior
<!-- Describe what the desired behavior would be. -->
## Minimal reproduction of the problem with instructions
<!-- Please share a repo, a gist, or step-by-step instructions. -->
## What is the motivation / use case for changing the behavior?
<!-- Describe the motivation or the concrete use case. -->
## Environment
<pre><code>
Nest version: X.Y.Z
<!-- Check whether this is still an issue in the most recent Nest version -->
For Tooling issues:
- Node version: XX <!-- run `node --version` -->
- Platform: <!-- Mac, Linux, Windows -->
Others:
<!-- Anything else relevant? Operating system version, IDE, package manager, ... -->
</code></pre>

View File

@@ -1,41 +0,0 @@
---
name: "\U0001F41B Bug Report"
about: "If something isn't working as expected \U0001F914."
title: ''
labels: 'type: potential issue :broken_heart:,needs triage'
assignees: ''
---
## Bug Report
## Current behavior
<!-- Describe how the issue manifests. -->
## Input Code
<!-- REPL or Repo link if applicable: -->
```ts
const your = (code) => here;
```
## Expected behavior
<!-- A clear and concise description of what you expected to happen (or code). -->
## Possible Solution
<!--- Only if you have suggestions on a fix for the bug -->
## Environment
<pre><code>
Nest version: X.Y.Z
<!-- Check whether this is still an issue in the most recent Nest version -->
For Tooling issues:
- Node version: XX <!-- run `node --version` -->
- Platform: <!-- Mac, Linux, Windows -->
Others:
<!-- Anything else relevant? Operating system version, IDE, package manager, ... -->
</code></pre>

View File

@@ -1,22 +0,0 @@
---
name: "\U0001F680 Feature Request"
about: "I have a suggestion \U0001F63B!"
title: ''
labels: 'type: enhancement :wolf:,needs triage'
assignees: ''
---
## Feature Request
## Is your feature request related to a problem? Please describe.
<!-- A clear and concise description of what the problem is. Ex. I have an issue when [...] -->
## Describe the solution you'd like
<!-- A clear and concise description of what you want to happen. Add any considered drawbacks. -->
## Teachability, Documentation, Adoption, Migration Strategy
<!-- If you can, explain how users will be able to use this and possibly write out a version the docs. Maybe a screenshot or design? -->
## What is the motivation / use case for changing the behavior?
<!-- Describe the motivation or the concrete use case. -->

View File

@@ -1,40 +0,0 @@
---
name: "\U0001F4A5 Regression"
about: Report an unexpected while upgrading your Nest application!
title: ''
labels: 'type: bug :sob:,needs triage'
assignees: ''
---
## Regression
<!-- First check out: https://docs.nestjs.com/migration-guide -->
## Potential Commit/PR that introduced the regression**
<!-- If you have time to investigate, what PR/date introduced this issue. -->
## Describe the regression
<!-- A clear and concise description of what the regression is. -->
## Input Code
<!--- If you have link to our REPL or a standalone repo please link that! -->
```ts
const your = (code) => here;
```
## Expected behavior/code
<!-- A clear and concise description of what you expected to happen (or code). -->
## Environment
<pre><code>
Nest version: A.B.C -> X.Y.Z
For Tooling issues:
- Node version: XX <!-- run `node --version` -->
- Platform: <!-- Mac, Linux, Windows -->
Others:
<!-- Anything else relevant? Operating system version, IDE, package manager, ... -->
</code></pre>

View File

@@ -1,14 +0,0 @@
---
name: "\U0001F917 Support Question"
about: "If you have a question \U0001F4AC, please check out our Discord or StackOverflow!"
title: ''
labels: 'type: question 🙌,needs triage'
assignees: ''
---
<!-- We primarily use GitHub as an issue tracker; for usage and support questions, please check out these resources below. Thanks! 😁. -->
* Discord Community Chat: https://discord.gg/G7Qnnhy
* StackOverflow: https://stackoverflow.com/questions/tagged/nestjs using the tag `nestjs`
* Twitter: If it's just a quick question you can ping our Twitter: https://twitter.com/nestframework

36
.github/lock.yml vendored
View File

@@ -1,36 +0,0 @@
# Number of days of inactivity before a closed issue or pull request is locked
daysUntilLock: 90
# Skip issues and pull requests created before a given timestamp. Timestamp must
# follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable
skipCreatedBefore: false
# Issues and pull requests with these labels will be ignored. Set to `[]` to disable
exemptLabels: []
# Label to add before locking, such as `outdated`. Set to `false` to disable
lockLabel: false
# Comment to post before locking. Set to `false` to disable
lockComment: >
This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.
# Assign `resolved` as the reason for locking. Set to `false` to disable
setLockReason: true
# Limit to only `issues` or `pulls`
# only: issues
# Optionally, specify configuration settings just for `issues` or `pulls`
# issues:
# exemptLabels:
# - help-wanted
# lockLabel: outdated
# pulls:
# daysUntilLock: 30
# Repository to extend settings from
# _extends: repo

6
.gitignore vendored
View File

@@ -5,7 +5,6 @@ node_modules/
/.idea
/.awcache
/.vscode
*.code-workspace
# bundle
packages/**/*.d.ts
@@ -27,9 +26,6 @@ yarn-error.log
# tests
/test
/benchmarks/memory
/coverage
/.nyc_output
/packages/graphql
/benchmarks/memory
build/config\.gypi
build/config\.gypi

View File

@@ -1,5 +1,4 @@
{
"singleQuote": true,
"arrowParens": "avoid",
"trailingComma": "all"
}
}

24
.travis.yml Normal file
View File

@@ -0,0 +1,24 @@
language: node_js
node_js:
- "8"
- "9"
- "10"
cache:
directories:
- "node_modules"
git:
depth: 5
addons:
firefox: "latest"
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
before_install:
- npm i -g npm@latest
install:
- npm ci
- npm run build:dev
script:
- npm run lint
- npm test
after_success: npm run coverage

View File

@@ -1,76 +0,0 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity and expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at support@nestjs.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq

120
CONTRIBUTING.md Executable file → Normal file
View File

@@ -4,14 +4,13 @@ We would love for you to contribute to Nest and help make it even better than it
today! As a contributor, here are the guidelines we would like you to follow:
<!--* [Code of Conduct](#coc)-->
- [Question or Problem?](#question)
- [Issues and Bugs](#issue)
- [Feature Requests](#feature)
- [Submission Guidelines](#submit)
- [Development Setup](#development)
- [Coding Rules](#rules)
- [Commit Message Guidelines](#commit)
* [Question or Problem?](#question)
* [Issues and Bugs](#issue)
* [Feature Requests](#feature)
* [Submission Guidelines](#submit)
* [Development Setup](#development)
* [Coding Rules](#rules)
* [Commit Message Guidelines](#commit)
<!-- - [Signing the CLA](#cla) -->
<!-- ## <a name="coc"></a> Code of Conduct
@@ -25,12 +24,12 @@ Stack Overflow is a much better place to ask questions since:
<!-- - there are thousands of people willing to help on Stack Overflow [maybe one day] -->
- questions and answers stay available for public viewing so your question / answer might help someone else
- Stack Overflow's voting system assures that the best answers are prominently visible.
* questions and answers stay available for public viewing so your question / answer might help someone else
* Stack Overflow's voting system assures that the best answers are prominently visible.
To save your and our time, we will systematically close all issues that are requests for general support and redirect people to Stack Overflow.
If you would like to chat about the question in real-time, you can reach out via [our discord channel][discord].
If you would like to chat about the question in real-time, you can reach out via [our gitter channel][gitter].
## <a name="issue"></a> Found a Bug?
@@ -45,10 +44,10 @@ Repository. If you would like to _implement_ a new feature, please submit an iss
a proposal for your work first, to be sure that we can use it.
Please consider what kind of change it is:
- For a **Major Feature**, first open an issue and outline your proposal so that it can be
* For a **Major Feature**, first open an issue and outline your proposal so that it can be
discussed. This will also allow us to better coordinate our efforts, prevent duplication of work,
and help you to craft the change so that it is successfully accepted into the project. For your issue name, please prefix your proposal with `[discussion]`, for example "[discussion]: your feature idea".
- **Small Features** can be crafted and directly [submitted as a Pull Request](#submit-pr).
* **Small Features** can be crafted and directly [submitted as a Pull Request](#submit-pr).
## <a name="submit"></a> Submission Guidelines
@@ -58,9 +57,9 @@ Before you submit an issue, please search the issue tracker, maybe an issue for
We want to fix all the issues as soon as possible, but before fixing a bug we need to reproduce and confirm it. In order to reproduce bugs we will systematically ask you to provide a minimal reproduction scenario using a repository or [Gist](https://gist.github.com/). Having a live, reproducible scenario gives us wealth of important information without going back & forth to you with additional questions like:
- version of NestJS used
- 3rd-party libraries and their versions
- and most importantly - a use-case that fails
* version of NestJS used
* 3rd-party libraries and their versions
* and most importantly - a use-case that fails
<!--
// TODO we need to create a playground, similar to plunkr
@@ -95,7 +94,7 @@ We cannot accept code without this.
1. Create your patch, **including appropriate test cases**.
1. Follow our [Coding Rules](#rules).
1. Run the full Nest test suite (see [common scripts](#common-scripts)),
1. Run the full Nest test suite, as described in the [developer documentation][dev-doc],
and ensure that all tests pass.
1. Commit your changes using a descriptive commit message that follows our
[commit message conventions](#commit). Adherence to these conventions
@@ -115,11 +114,11 @@ We cannot accept code without this.
1. In GitHub, send a pull request to `nestjs:master`.
- If we suggest changes then:
* If we suggest changes then:
- Make the required updates.
- Re-run the Nest test suites to ensure tests are still passing.
- Rebase your branch and force push to your GitHub repository (this will update your Pull Request):
* Make the required updates.
* Re-run the Nest test suites to ensure tests are still passing.
* Rebase your branch and force push to your GitHub repository (this will update your Pull Request):
```shell
git rebase master -i
@@ -133,25 +132,25 @@ That's it! Thank you for your contribution!
After your pull request is merged, you can safely delete your branch and pull the changes
from the main (upstream) repository:
- Delete the remote branch on GitHub either through the GitHub web UI or your local shell as follows:
* Delete the remote branch on GitHub either through the GitHub web UI or your local shell as follows:
```shell
git push origin --delete my-fix-branch
```
- Check out the master branch:
* Check out the master branch:
```shell
git checkout master -f
```
- Delete the local branch:
* Delete the local branch:
```shell
git branch -D my-fix-branch
```
- Update your master with the latest upstream version:
* Update your master with the latest upstream version:
```shell
git pull --ff upstream master
@@ -175,12 +174,17 @@ $ sh scripts/prepare.sh
That will compile fresh packages and afterward, move them to all `sample` directories as well as integration tests.
### <a name="common-scripts"></a>Commonly used NPM scripts
### Commonly used NPM scripts
```bash
# build all packages and move to "sample" directories
# build all packages and put them near to their source .ts files
$ npm run build
# build all packages and move to "sample" and "integration" directories
# if cross-packages breaking changes were performed you may face irrelevant errors
# in order to verify the build, you can run this command again then
$ npm run build:dev
# run the full unit tests suite
$ npm run test
@@ -190,9 +194,6 @@ $ sh scripts/run-integration.sh
# run linter
$ npm run lint
# build all packages and put them near to their source .ts files
$ npm run build:prod
```
## <a name="rules"></a> Coding Rules
@@ -203,9 +204,10 @@ To ensure consistency throughout the source code, keep these rules in mind as yo
// We're working on auto-documentation.
* All public API methods **must be documented**. (Details TBC). -->
- All features or bug fixes **must be tested** by one or more specs (unit-tests).
- We follow [Google's JavaScript Style Guide][js-style-guide], but wrap all code at
**100 characters**. An automated formatter is available (`npm run format`).
* All features or bug fixes **must be tested** by one or more specs (unit-tests).
* We follow [Google's JavaScript Style Guide][js-style-guide], but wrap all code at
**100 characters**. An automated formatter is available, see
[DEVELOPER.md](docs/DEVELOPER.md#clang-format).
## <a name="commit"></a> Commit Message Guidelines
@@ -236,8 +238,8 @@ Footer should contain a [closing reference to an issue](https://help.github.com/
Samples: (even more [samples](https://github.com/nestjs/nest/commits/master))
```
docs(changelog): update change log to beta.5
bugfix(core): need to depend on latest rxjs and zone.js
docs(changelog) update change log to beta.5
bugfix(core) need to depend on latest rxjs and zone.js
```
### Revert
@@ -248,15 +250,15 @@ If the commit reverts a previous commit, it should begin with `revert:`, followe
Must be one of the following:
- **build**: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
- **ci**: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
- **docs**: Documentation only changes
- **feature**: A new feature
- **fix**: A bug fix
- **perf**: A code change that improves performance
- **refactor**: A code change that neither fixes a bug nor adds a feature
- **style**: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
- **test**: Adding missing tests or correcting existing tests
* **build**: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
* **ci**: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
* **docs**: Documentation only changes
* **feature**: A new feature
* **bugfix**: A bug fix
* **perf**: A code change that improves performance
* **refactor**: A code change that neither fixes a bug nor adds a feature
* **style**: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
* **test**: Adding missing tests or correcting existing tests
### Scope
@@ -264,28 +266,28 @@ The scope should be the name of the npm package affected (as perceived by person
The following is the list of supported scopes:
- **common**
- **core**
- **sample**
- **microservices**
- **testing**
- **websockets**
* **common**
* **core**
* **sample**
* **microservices**
* **testing**
* **websockets**
There are currently a few exceptions to the "use package name" rule:
- **packaging**: used for changes that change the npm package layout in all of our packages, e.g. public path changes, package.json changes done to all packages, d.ts file/format changes, changes to bundles, etc.
- **changelog**: used for updating the release notes in CHANGELOG.md
- **sample/#**: for the example apps directory, replacing # with the example app number
- none/empty string: useful for `style`, `test` and `refactor` changes that are done across all packages (e.g. `style: add missing semicolons`)
<!-- * **aio**: used for docs-app (angular.io) related changes within the /aio directory of the repo -->
* **packaging**: used for changes that change the npm package layout in all of our packages, e.g. public path changes, package.json changes done to all packages, d.ts file/format changes, changes to bundles, etc.
* **changelog**: used for updating the release notes in CHANGELOG.md
* **sample/#**: for the example apps directory, replacing # with the example app number
* none/empty string: useful for `style`, `test` and `refactor` changes that are done across all packages (e.g. `style: add missing semicolons`)
<!-- * **aio**: used for docs-app (angular.io) related changes within the /aio directory of the repo -->
### Subject
The subject contains succinct description of the change:
- use the imperative, present tense: "change" not "changed" nor "changes"
- don't capitalize first letter
- no dot (.) at the end
* use the imperative, present tense: "change" not "changed" nor "changes"
* don't capitalize first letter
* no dot (.) at the end
### Body
@@ -318,7 +320,7 @@ changes to be accepted, the CLA must be signed. It's a quick process, we promise
[corporate-cla]: http://code.google.com/legal/corporate-cla-v1.0.html
[dev-doc]: https://github.com/nestjs/nest/blob/master/docs/DEVELOPER.md
[github]: https://github.com/nestjs/nest
[discord]: https://discordapp.com/invite/G7Qnnhy
[gitter]: https://gitter.im/nestjs/nest
[individual-cla]: http://code.google.com/legal/individual-cla-v1.0.html
[js-style-guide]: https://google.github.io/styleguide/jsguide.html
[jsfiddle]: http://jsfiddle.net

View File

@@ -1,6 +1,6 @@
(The MIT License)
Copyright (c) 2017-2019 Kamil Myśliwiec <http://kamilmysliwiec.com>
Copyright (c) 2017 Kamil Myśliwiec <http://kamilmysliwiec.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@@ -1,85 +1,73 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a>
</p>
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
[circleci-url]: https://circleci.com/gh/nestjs/nest
[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
[travis-url]: https://travis-ci.org/nestjs/nest
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
[linux-url]: https://travis-ci.org/nestjs/nest
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
<p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable server-side applications, heavily inspired by <a href="https://angular.io" target="blank">Angular</a>.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a>
<a href="https://travis-ci.org/nestjs/nest"><img src="https://api.travis-ci.org/nestjs/nest.svg?branch=master" alt="Travis" /></a>
<a href="https://travis-ci.org/nestjs/nest"><img src="https://img.shields.io/travis/nestjs/nest/master.svg?label=linux" alt="Linux" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#8" alt="Coverage" /></a>
<a href="https://gitter.im/nestjs/nestjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge"><img src="https://badges.gitter.im/nestjs/nestjs.svg" alt="Gitter" /></a>
<a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
<a href="https://twitter.com/nestframework"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
## Description
Nest is a framework for building efficient, scalable <a href="http://nodejs.org" target="_blank">Node.js</a> server-side applications. It uses modern JavaScript, is built with <a href="http://www.typescriptlang.org" target="_blank">TypeScript</a> (preserves compatibility with pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
<p>Nest is a framework for building efficient, scalable <a href="http://nodejs.org" target="_blank">Node.js</a> server-side applications. It uses modern JavaScript, is built with <a href="http://www.typescriptlang.org" target="_blank">TypeScript</a> (preserves compatibility with pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).</p>
<p>Under the hood, Nest makes use of <a href="https://expressjs.com/" target="_blank">Express</a>, but also, provides compatibility with a wide range of other libraries, like e.g. <a href="https://github.com/fastify/fastify" target="_blank">Fastify</a>, allowing for easy use of the myriad third-party plugins which are available.</p>
<p>Under the hood, Nest makes use of <a href="https://expressjs.com/" target="_blank">Express</a>, but also, provides compatibility with a wide range of other libraries, like e.g. <a href="https://github.com/fastify/fastify" target="blank">Fastify</a>, allowing for easy use of the myriad third-party plugins which are available.</p>
## Philosophy
<p>In recent years, thanks to Node.js, JavaScript has become the “lingua franca” of the web for both front and backend applications, giving rise to awesome projects like <a href="https://angular.io/" target="_blank">Angular</a>, <a href="https://github.com/facebook/react" target="_blank">React</a> and <a href="https://github.com/vuejs/vue" target="_blank">Vue</a> which improve developer productivity and enable the construction of fast, testable, extensible frontend applications. However, on the server-side, while there are a lot of superb libraries, helpers and tools for Node, none of them effectively solve the main problem - the architecture.</p>
<p>Nest aims to provide an application architecture out of the box which allows for effortless creation of highly testable, scalable, loosely coupled and easily maintainable applications. The architecture is heavily inspired by Angular.</p>
<p>Nest aims to provide an application architecture out of the box which allows for effortless creation of highly testable, scalable, loosely coupled and easily maintainable applications.</p>
## Getting started
* To check out the [guide](https://docs.nestjs.com), visit [docs.nestjs.com](https://docs.nestjs.com). :books:
* 要查看中文 [指南](readme_zh.md), 请访问 [docs.nestjs.cn](https://docs.nestjs.cn). :books:
## Consulting
With official support, you can get expert help straight from Nest core team. We provide dedicated technical support, migration strategies, advice on best practices (and design decisions), PR reviews, and team augmentation. Read more about [support here](https://enterprise.nestjs.com).
## Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
#### Principal Sponsor
<a href="https://valor-software.com/" target="_blank"><img src="https://docs.nestjs.com/assets/sponsors/valor-software.png" width="320" /></a>
<a href="https://valor-software.com/"><img src="https://docs.nestjs.com/assets/sponsors/valor-software.png" width="320" /></a>
#### Gold Sponsors
<a href="http://xtremis.com/"><img src="https://nestjs.com/img/logo-xtremis.svg" width="220" /></a>
#### Silver Sponsors
<a href="https://neoteric.eu/" target="_blank"><img src="https://nestjs.com/img/neoteric-cut.png" width="120" valign="middle" /></a> &nbsp;
<a href="http://gojob.com" target="_blank"><img src="http://nestjs.com/img/gojob-logo.png" valign="middle" height="95" /></a> &nbsp;
<a href="https://trilon.io" target="_blank"><img src="https://nestjs.com/img/trilon.svg" width="150" valign="middle" /></a> &nbsp;
<a href="http://www.leogistics.com" target="_blank"><img src="https://nestjs.com/img/leogistics-logo.jpeg" width="150" valign="middle" /></a>
<a href="https://neoteric.eu/"><img src="https://nestjs.com/img/neoteric-cut.png" width="120" /></a> &nbsp;
<a href="http://gojob.com"><img src="http://nestjs.com/img/gojob-logo.png" valign="bottom" height="95" /></a> &nbsp; <a href="https://www.swingdev.io"><img src="https://nestjs.com/img/swingdev-logo.svg#1" width="150" /> </a>
#### Sponsors
<a href="https://www.swingdev.io" target="_blank"><img src="https://nestjs.com/img/swingdev-logo.svg#1" width="110" valign="middle" /> </a> &nbsp; <a href="https://blueanchor.io/" target="_blank"><img src="https://nestjs.com/img/blueanchor.png" width="150" valign="middle" /></a> &nbsp;
<a href="https://www.novologic.com/" target="_blank"><img src="https://nestjs.com/img/novologic.png" width="110" valign="middle" /></a> &nbsp; <a href="https://hostpresto.com" target="_blank"><img src="https://nestjs.com/img/hostpresto.png" height="24" valign="middle" /></a>
<a href="https://ever.co/" target="_blank"><img src="https://nestjs.com/img/ever-logo.png" height="14" valign="middle" /></a> &nbsp;
<a href="https://buddy.works/" target="_blank"><img src="https://nestjs.com/img/buddy-logo.svg" height="25" valign="middle" /></a> &nbsp;
<a href="https://blokt.com" target="_blank"><img src="https://nestjs.com/img/blokt-logo.png" height="25" valign="middle" /></a> &nbsp; <a href="https://genuinebee.com/" target="_blank"><img src="https://nestjs.com/img/genuinebee.svg" height="27" valign="middle" /></a> &nbsp; <a href="http://architectnow.net/" target="_blank"><img src="https://nestjs.com/img/architectnow.png" height="20" valign="middle" /></a> &nbsp; <a href="https://quander.io/" target="_blank"><img src="https://nestjs.com/img/quander.png" height="22" valign="middle" /></a> &nbsp; <a href="https://mantro.net/" target="_blank"><img src="https://nestjs.com/img/mantro-logo.svg" height="19" valign="middle" /></a> &nbsp; <a href="https://triplebyte.com/" target="_blank"><img src="https://nestjs.com/img/triplebyte.png" height="20" valign="middle" /></a> &nbsp;
<a href="https://reposit.co.uk/" target="_blank"><img src="https://nestjs.com/img/reposit-logo.png" height="18" valign="middle" /></a> &nbsp; &nbsp;
<a href="https://nearpod.com/" target="_blank"><img src="https://nestjs.com/img/nearpod-logo.svg" width="100" valign="middle" /></a> &nbsp;
<a href="https://clay.global/" target="_blank"><img src="https://nestjs.com/img/clay-logo.svg" width="75" valign="middle" /></a> &nbsp;
<a href="https://firesticktricks.com" target="_blank"><img src="https://nestjs.com/img/firesticktricks-logo.png" width="120" valign="middle" /></a> &nbsp;
<a href="https://www.codeguesser.co.uk" target="_blank"><img src="https://nestjs.com/img/codeguesser-logo.svg" width="120" valign="middle" /></a> &nbsp;
<a href="https://tekhattan.com" target="_blank"><img src="https://nestjs.com/img/tekhattan-logo.png" width="110" valign="middle" /></a> &nbsp;
<a href="https://f-a.nz/" target="_blank"><img src="https://nestjs.com/img/franz.svg" width="80" valign="middle" /></a> &nbsp;
<a href="https://sparkfabrik.com/" target="_blank"><img src="https://nestjs.com/img/sparkfabrik-logo.png" width="120" valign="middle" /></a>
<a href="https://scal.io"><img src="https://nestjs.com/img/scalio-logo.svg" width="110" /></a> &nbsp; <a href="http://angularity.io"><img src="http://angularity.io/media/logo.svg" height="30" /></a> &nbsp; <!--<a href="https://keycdn.com"><img src="https://nestjs.com/img/keycdn.svg" height="30" /></a> &nbsp;--> <a href="https://hostpresto.com"><img src="https://nestjs.com/img/hostpresto.png" height="30" /></a> &nbsp; <a href="https://genuinebee.com/"><img src="https://nestjs.com/img/genuinebee.svg" height="38" /></a> &nbsp; <a href="http://architectnow.net/"><img src="https://nestjs.com/img/architectnow.png" height="24" /></a> &nbsp; <a href="https://quander.io/"><img src="https://nestjs.com/img/quander.png" height="28" /></a>
## Backers
<a href="https://opencollective.com/nest" target="_blank"><img src="https://opencollective.com/nest/backers.svg?width=1000"></a>
<a href="https://opencollective.com/nest"><img src="https://opencollective.com/nest/backers.svg?width=890"></a>
## Stay in touch
* Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
* Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
* Website - [https://nestjs.com](https://nestjs.com/)
* Twitter - [@nestframework](https://twitter.com/nestframework)

View File

@@ -1,14 +1,9 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const core_1 = require('@nestjs/core');
const fastify_platform_1 = require('@nestjs/platform-fastify');
const app_module_1 = require('./nest/app.module');
core_1.NestFactory.create(
app_module_1.AppModule,
new fastify_platform_1.FastifyAdapter(),
{
logger: false,
bodyParser: false,
},
).then(app => app.listen(3000));
core_1.NestFactory.create(app_module_1.AppModule, new core_1.FastifyAdapter(), {
logger: false,
bodyParser: false,
}).then(app => app.listen(3000));
//# sourceMappingURL=main.js.map

View File

@@ -7,8 +7,8 @@ var __decorate =
c < 3
? target
: desc === null
? (desc = Object.getOwnPropertyDescriptor(target, key))
: desc,
? (desc = Object.getOwnPropertyDescriptor(target, key))
: desc,
d;
if (typeof Reflect === 'object' && typeof Reflect.decorate === 'function')
r = Reflect.decorate(decorators, target, key, desc);
@@ -42,6 +42,9 @@ __decorate(
'root',
null,
);
AppController = __decorate([common_1.Controller()], AppController);
AppController = __decorate(
[common_1.Controller({ scope: common_1.Scope.REQUEST })],
AppController,
);
exports.AppController = AppController;
//# sourceMappingURL=app.controller.js.map

14
benchmarks/run.sh Normal file
View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
echo 'Library:' $1
node $1 &
pid=$!
sleep 2
wrk 'http://localhost:3000' \
-d 10 \
-c 1024 \
-t 8
kill $pid

23
benchmarks/run_all.sh Normal file
View File

@@ -0,0 +1,23 @@
#!/usr/bin/env bash
: > all_output.txt
lib=(express fastify nest nest-fastify)
for item in ${lib[*]}
do
echo '-----------------------' >> all_output.txt
echo $item >> all_output.txt
echo '-----------------------' >> all_output.txt
node $item &
pid=$!
sleep 2
wrk 'http://localhost:3000' \
-d 10 \
-c 1024 \
-t 8 >> all_output.txt
kill $pid
done

View File

@@ -1,16 +1,117 @@
'use strict';
/**
* Load the TypeScript compiler, then load the TypeScript gulpfile which simply loads all
* the tasks. The tasks are really inside tools/gulp/tasks.
*/
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const ts = require('gulp-typescript');
const sourcemaps = require('gulp-sourcemaps');
const clean = require('gulp-clean');
const deleteEmpty = require('delete-empty');
const projectDir = __dirname;
const tsconfigPath = path.join(projectDir, 'tools/gulp/tsconfig.json');
const packages = {
common: ts.createProject('packages/common/tsconfig.json'),
core: ts.createProject('packages/core/tsconfig.json'),
microservices: ts.createProject('packages/microservices/tsconfig.json'),
websockets: ts.createProject('packages/websockets/tsconfig.json'),
testing: ts.createProject('packages/testing/tsconfig.json'),
'platform-express': ts.createProject(
'packages/platform-express/tsconfig.json',
),
'platform-fastify': ts.createProject(
'packages/platform-fastify/tsconfig.json',
),
'platform-socket.io': ts.createProject(
'packages/platform-socket.io/tsconfig.json',
),
'platform-ws': ts.createProject('packages/platform-ws/tsconfig.json'),
};
const modules = Object.keys(packages);
const source = 'packages';
const distId = process.argv.indexOf('--dist');
const dist = distId < 0 ? source : process.argv[distId + 1];
require('ts-node').register({
project: tsconfigPath
gulp.task('default', function() {
modules.forEach(module => {
gulp.watch(
[`${source}/${module}/**/*.ts`, `${source}/${module}/*.ts`],
[module],
);
});
});
require('./tools/gulp/gulpfile');
gulp.task('copy-misc', function() {
return gulp
.src(['Readme.md', 'LICENSE', '.npmignore'])
.pipe(gulp.dest(`${source}/common`))
.pipe(gulp.dest(`${source}/core`))
.pipe(gulp.dest(`${source}/microservices`))
.pipe(gulp.dest(`${source}/websockets`))
.pipe(gulp.dest(`${source}/testing`))
.pipe(gulp.dest(`${source}/platform-fastify`))
.pipe(gulp.dest(`${source}/platform-express`))
.pipe(gulp.dest(`${source}/platform-ws`))
.pipe(gulp.dest(`${source}/platform-socket.io`));
});
gulp.task('clean:output', function() {
return gulp
.src(
[`${source}/**/*.js`, `${source}/**/*.d.ts`, `${source}/**/*.js.map`],
{
read: false,
},
)
.pipe(clean());
});
gulp.task('clean:dirs', function(done) {
deleteEmpty.sync(`${source}/`);
done();
});
gulp.task('clean:bundle', gulp.series('clean:output', 'clean:dirs'));
modules.forEach(module => {
gulp.task(module, () => {
return packages[module]
.src()
.pipe(packages[module]())
.pipe(gulp.dest(`${dist}/${module}`));
});
});
modules.forEach(module => {
gulp.task(module + ':dev', () => {
return packages[module]
.src()
.pipe(sourcemaps.init())
.pipe(packages[module]())
.pipe(
sourcemaps.mapSources(sourcePath => './' + sourcePath.split('/').pop()),
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dist}/${module}`));
});
});
gulp.task('common:dev', gulp.series(modules.map(module => module + ':dev')));
gulp.task('build', gulp.series(modules));
gulp.task('build:dev', gulp.series('common:dev'));
function getFolders(dir) {
return fs.readdirSync(dir).filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
gulp.task('move', function() {
const getDirs = base => getFolders(base).map(path => `${base}/${path}`);
const examplesDirs = getDirs('sample');
const integrationDirs = getDirs('integration');
const directories = examplesDirs.concat(integrationDirs);
let stream = gulp.src(['node_modules/@nestjs/**/*']);
directories.forEach(dir => {
stream = stream.pipe(gulp.dest(dir + '/node_modules/@nestjs'));
});
return stream;
});

View File

@@ -23,7 +23,8 @@ services:
- "9001:9001"
restart: always
mysql:
image: mysql:5.7.29
image: mysql:5.7.22
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
@@ -31,42 +32,15 @@ services:
- "3306:3306"
restart: always
mongodb:
container_name: test-mongodb
image: mongo:latest
environment:
- MONGODB_DATABASE="test"
ports:
- 27017:27017
rabbit:
container_name: test-rabbit
hostname: rabbit
image: "rabbitmq:management"
ports:
- "15672:15672"
- "5672:5672"
tty: true
zookeeper:
container_name: test-zookeeper
hostname: zookeeper
image: confluentinc/cp-zookeeper:5.3.2
ports:
- "2181:2181"
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
kafka:
container_name: test-kafka
hostname: kafka
image: confluentinc/cp-kafka:5.3.2
depends_on:
- zookeeper
ports:
- "29092:29092"
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
tty: true

View File

@@ -1,36 +0,0 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
describe('GraphQL - Code-first', () => {
let app: INestApplication;
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
app = module.createNestApplication();
await app.init();
});
it(`should return query result`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: '{\n recipes {\n id\n }\n}\n',
})
.expect(200, {
data: {
recipes: [],
},
});
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -1,49 +0,0 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
describe('GraphQL - Guards', () => {
let app: INestApplication;
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
app = module.createNestApplication();
await app.init();
});
it(`should throw an error`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: '{\n recipe(id: "3") {\n id\n }\n}\n',
})
.expect(200, {
data: null,
errors: [
{
message: 'Unauthorized error',
locations: [
{
line: 2,
column: 3,
},
],
path: ['recipe'],
extensions: {
code: 'INTERNAL_SERVER_ERROR',
},
},
],
});
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -1,62 +0,0 @@
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
describe('GraphQL Pipes', () => {
let app: INestApplication;
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
app = module.createNestApplication();
app.useGlobalPipes(new ValidationPipe());
await app.init();
});
it(`should throw an error`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query:
'mutation {\n addRecipe(newRecipeData: {title: "test", ingredients: []}) {\n id\n }\n}\n',
})
.expect(200, {
data: null,
errors: [
{
extensions: {
code: 'INTERNAL_SERVER_ERROR',
exception: {
message: 'Bad Request Exception',
response: {
message: [
'description must be longer than or equal to 30 characters',
],
error: 'Bad Request',
statusCode: 400,
},
status: 400,
},
},
locations: [
{
column: 3,
line: 2,
},
],
message: 'Bad Request Exception',
path: ['addRecipe'],
},
],
});
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -1,34 +0,0 @@
# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------
"""Date custom scalar type"""
scalar Date
type Mutation {
addRecipe(newRecipeData: NewRecipeInput!): Recipe!
removeRecipe(id: String!): Boolean!
}
input NewRecipeInput {
title: String!
description: String
ingredients: [String!]!
}
type Query {
recipe(id: String!): Recipe!
recipes(skip: Int = 0, take: Int = 25): [Recipe!]!
}
type Recipe {
id: ID!
title: String!
description: String
creationDate: Date!
ingredients: [String!]!
}
type Subscription {
recipeAdded: Recipe!
}

View File

@@ -1,19 +0,0 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { join } from 'path';
import { RecipesModule } from './recipes/recipes.module';
@Module({
imports: [
RecipesModule,
GraphQLModule.forRoot({
debug: false,
installSubscriptionHandlers: true,
autoSchemaFile: join(
process.cwd(),
'integration/graphql-code-first/schema.gql',
),
}),
],
})
export class ApplicationModule {}

View File

@@ -1,9 +0,0 @@
import { ArgumentsHost, Catch, UnauthorizedException } from '@nestjs/common';
import { GqlExceptionFilter } from '@nestjs/graphql';
@Catch(UnauthorizedException)
export class UnauthorizedFilter implements GqlExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
return new Error('Unauthorized error');
}
}

View File

@@ -1,18 +0,0 @@
import {
CanActivate,
ExecutionContext,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
@Injectable()
export class AuthGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
const gqlContext = GqlExecutionContext.create(context);
if (gqlContext) {
throw new UnauthorizedException();
}
return true;
}
}

View File

@@ -1,18 +0,0 @@
import { Field, InputType } from '@nestjs/graphql';
import { Type } from 'class-transformer';
import { Length, MaxLength } from 'class-validator';
@InputType()
export class NewRecipeInput {
@Field()
@MaxLength(30)
title: string;
@Field({ nullable: true })
@Length(30, 255)
description?: string;
@Type(() => String)
@Field(type => [String])
ingredients: string[];
}

View File

@@ -1,14 +0,0 @@
import { ArgsType, Field, Int } from '@nestjs/graphql';
import { Max, Min } from 'class-validator';
@ArgsType()
export class RecipesArgs {
@Field(type => Int)
@Min(0)
skip = 0;
@Field(type => Int)
@Min(1)
@Max(50)
take = 25;
}

View File

@@ -1,19 +0,0 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class Recipe {
@Field(type => ID)
id: string;
@Field()
title: string;
@Field({ nullable: true })
description?: string;
@Field()
creationDate: Date;
@Field(type => [String])
ingredients: string[];
}

View File

@@ -1,19 +0,0 @@
import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { UnauthorizedFilter } from '../common/filters/unauthorized.filter';
import { DateScalar } from '../common/scalars/date.scalar';
import { RecipesResolver } from './recipes.resolver';
import { RecipesService } from './recipes.service';
@Module({
providers: [
RecipesResolver,
RecipesService,
DateScalar,
{
provide: APP_FILTER,
useClass: UnauthorizedFilter,
},
],
})
export class RecipesModule {}

View File

@@ -1,51 +0,0 @@
import { NotFoundException, UseGuards, UseInterceptors } from '@nestjs/common';
import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { PubSub } from 'apollo-server-express';
import { AuthGuard } from '../common/guards/auth.guard';
import { DataInterceptor } from '../common/interceptors/data.interceptor';
import { NewRecipeInput } from './dto/new-recipe.input';
import { RecipesArgs } from './dto/recipes.args';
import { Recipe } from './models/recipe';
import { RecipesService } from './recipes.service';
const pubSub = new PubSub();
@UseInterceptors(DataInterceptor)
@Resolver(of => Recipe)
export class RecipesResolver {
constructor(private readonly recipesService: RecipesService) {}
@UseGuards(AuthGuard)
@Query(returns => Recipe)
async recipe(@Args('id') id: string): Promise<Recipe> {
const recipe = await this.recipesService.findOneById(id);
if (!recipe) {
throw new NotFoundException(id);
}
return recipe;
}
@Query(returns => [Recipe])
recipes(@Args() recipesArgs: RecipesArgs): Promise<Recipe[]> {
return this.recipesService.findAll(recipesArgs);
}
@Mutation(returns => Recipe)
async addRecipe(
@Args('newRecipeData') newRecipeData: NewRecipeInput,
): Promise<Recipe> {
const recipe = await this.recipesService.create(newRecipeData);
pubSub.publish('recipeAdded', { recipeAdded: recipe });
return recipe;
}
@Mutation(returns => Boolean)
async removeRecipe(@Args('id') id: string) {
return this.recipesService.remove(id);
}
@Subscription(returns => Recipe)
recipeAdded() {
return pubSub.asyncIterator('recipeAdded');
}
}

View File

@@ -1,31 +0,0 @@
import { Injectable } from '@nestjs/common';
import { NewRecipeInput } from './dto/new-recipe.input';
import { RecipesArgs } from './dto/recipes.args';
import { Recipe } from './models/recipe';
@Injectable()
export class RecipesService {
/**
* MOCK
* Put some real business logic here
* Left for demonstration purposes
*/
async create(data: NewRecipeInput): Promise<Recipe> {
return {
id: 3,
} as any;
}
async findOneById(id: string): Promise<Recipe> {
return {} as any;
}
async findAll(recipesArgs: RecipesArgs): Promise<Recipe[]> {
return [] as Recipe[];
}
async remove(id: string): Promise<boolean> {
return true;
}
}

View File

@@ -1,60 +0,0 @@
import { INestApplication } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import { join } from 'path';
import * as request from 'supertest';
import { CatsRequestScopedService } from '../src/cats/cats-request-scoped.service';
import { CatsModule } from '../src/cats/cats.module';
describe('GraphQL request scoped', () => {
let app: INestApplication;
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
CatsModule.enableRequestScope(),
GraphQLModule.forRoot({
typePaths: [join(__dirname, '..', 'src', '**', '*.graphql')],
}),
],
}).compile();
app = module.createNestApplication();
await app.init();
const performHttpCall = end =>
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: '{\n getCats {\n id\n }\n}\n',
})
.expect(200, {
data: {
getCats: [
{
id: 1,
},
],
},
})
.end((err, res) => {
if (err) return end(err);
end();
});
await new Promise(resolve => performHttpCall(resolve));
await new Promise(resolve => performHttpCall(resolve));
await new Promise(resolve => performHttpCall(resolve));
});
it(`should create resolver for each incoming request`, () => {
expect(CatsRequestScopedService.COUNTER).to.be.eql(3);
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -1,25 +0,0 @@
import { Injectable, Scope } from '@nestjs/common';
import { Cat } from './interfaces/cat.interface';
@Injectable({ scope: Scope.REQUEST })
export class CatsRequestScopedService {
static COUNTER = 0;
private readonly cats: Cat[] = [{ id: 1, name: 'Cat', age: 5 }];
constructor() {
CatsRequestScopedService.COUNTER++;
}
create(cat: Cat): Cat {
this.cats.push(cat);
return cat;
}
findAll(): Cat[] {
return this.cats;
}
findOneById(id: number): Cat {
return this.cats.find(cat => cat.id === id);
}
}

View File

@@ -1,22 +0,0 @@
import { DynamicModule, Module, Scope } from '@nestjs/common';
import { CatsRequestScopedService } from './cats-request-scoped.service';
import { CatsResolvers } from './cats.resolvers';
import { CatsService } from './cats.service';
@Module({
providers: [CatsService, CatsResolvers],
})
export class CatsModule {
static enableRequestScope(): DynamicModule {
return {
module: CatsModule,
providers: [
{
provide: CatsService,
useClass: CatsRequestScopedService,
scope: Scope.REQUEST,
},
],
};
}
}

View File

@@ -1,23 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"allowJs": true,
"outDir": "./dist",
"skipLibCheck": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}

View File

@@ -1,4 +1,4 @@
import { INestApplication } from '@nestjs/common';
/*import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as request from 'supertest';
import { AsyncClassApplicationModule } from '../src/async-options-class.module';
@@ -36,3 +36,4 @@ describe('GraphQL (async class)', () => {
await app.close();
});
});
*/

View File

@@ -1,4 +1,4 @@
import { INestApplication } from '@nestjs/common';
/*import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as request from 'supertest';
import { AsyncExistingApplicationModule } from '../src/async-options-existing.module';
@@ -36,3 +36,4 @@ describe('GraphQL (async existing)', () => {
await app.close();
});
});
*/

View File

@@ -1,4 +1,4 @@
import { INestApplication } from '@nestjs/common';
/*import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as request from 'supertest';
import { AsyncApplicationModule } from '../src/async-options.module';
@@ -34,3 +34,4 @@ describe('GraphQL (async configuration)', () => {
await app.close();
});
});
*/

View File

@@ -1,4 +1,4 @@
import { INestApplication } from '@nestjs/common';
/*import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
@@ -38,3 +38,4 @@ describe('GraphQL', () => {
await app.close();
});
});
*/

1959
integration/graphql/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,29 @@
{
"name": "nest-typescript-starter",
"version": "1.0.0",
"description": "Nest TypeScript starter repository",
"license": "MIT",
"scripts": {
"start": "ts-node src/main",
"prestart:prod": "tsc",
"start:prod": "node dist/main.js"
},
"dependencies": {
"@nestjs/common": "^5.1.0",
"@nestjs/core": "^5.1.0",
"@nestjs/graphql": "^5.0.0",
"apollo-server-express": "^2.0.4",
"graphql": "^0.13.2",
"graphql-tools": "^2.11.0",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"subscriptions-transport-ws": "^0.9.5",
"typescript": "^3.1.0",
"ws": "^4.0.0"
},
"devDependencies": {
"@types/node": "^7.0.41",
"ts-node": "^6.0.0",
"tslint": "^5.9.1"
}
}

View File

@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CatsResolvers } from './cats.resolvers';
@Module({
providers: [CatsService, CatsResolvers],
})
export class CatsModule {}

View File

@@ -14,7 +14,7 @@ export class CatsResolvers {
@Query()
@UseGuards(CatsGuard)
async getCats() {
return this.catsService.findAll();
return await this.catsService.findAll();
}
@Query('cat')
@@ -22,7 +22,7 @@ export class CatsResolvers {
@Args('id', ParseIntPipe)
id: number,
): Promise<Cat> {
return this.catsService.findOneById(id);
return await this.catsService.findOneById(id);
}
@Mutation('createCat')
@@ -34,6 +34,8 @@ export class CatsResolvers {
@Subscription('catCreated')
catCreated() {
return pubSub.asyncIterator('catCreated');
return {
subscribe: () => pubSub.asyncIterator('catCreated'),
};
}
}

View File

@@ -3,13 +3,8 @@ import { Cat } from './interfaces/cat.interface';
@Injectable()
export class CatsService {
static COUNTER = 0;
private readonly cats: Cat[] = [{ id: 1, name: 'Cat', age: 5 }];
constructor() {
CatsService.COUNTER++;
}
create(cat: Cat): Cat {
this.cats.push(cat);
return cat;

View File

@@ -0,0 +1,54 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {
"no-unused-expression": true
},
"rules": {
"eofline": false,
"quotemark": [
true,
"single"
],
"indent": false,
"ordered-imports": [
false
],
"max-line-length": [
150
],
"member-ordering": [
false
],
"curly": false,
"interface-name": [
false
],
"array-type": [
false
],
"member-access": [
false
],
"no-empty-interface": false,
"no-empty": false,
"arrow-parens": false,
"object-literal-sort-keys": false,
"no-unused-expression": false,
"max-classes-per-file": [
false
],
"variable-name": [
false
],
"one-line": [
false
],
"one-variable-per-declaration": [
false
]
},
"rulesDirectory": []
}

View File

@@ -1,6 +1,6 @@
import { HttpStatus, INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication, HttpStatus } from '@nestjs/common';
import { ErrorsController } from '../src/errors/errors.controller';
describe('Error messages', () => {
@@ -10,7 +10,8 @@ describe('Error messages', () => {
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [ErrorsController],
}).compile();
})
.compile();
app = module.createNestApplication();
server = app.getHttpServer();
@@ -18,19 +19,25 @@ describe('Error messages', () => {
});
it(`/GET`, () => {
return request(server).get('/sync').expect(HttpStatus.BAD_REQUEST).expect({
statusCode: 400,
error: 'Bad Request',
message: 'Integration test',
});
return request(server)
.get('/sync')
.expect(HttpStatus.BAD_REQUEST)
.expect({
statusCode: 400,
error: 'Bad Request',
message: 'Integration test'
});
});
it(`/GET (Promise/async)`, () => {
return request(server).get('/async').expect(HttpStatus.BAD_REQUEST).expect({
statusCode: 400,
error: 'Bad Request',
message: 'Integration test',
});
return request(server)
.get('/async')
.expect(HttpStatus.BAD_REQUEST)
.expect({
statusCode: 400,
error: 'Bad Request',
message: 'Integration test'
});
});
afterEach(async () => {

View File

@@ -1,121 +0,0 @@
import {
Controller,
Get,
INestApplication,
MiddlewareConsumer,
Module,
Post,
RequestMethod,
} from '@nestjs/common';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
const RETURN_VALUE = 'test';
const MIDDLEWARE_VALUE = 'middleware';
@Controller()
class TestController {
@Get('test')
test() {
return RETURN_VALUE;
}
@Get('test2')
test2() {
return RETURN_VALUE;
}
@Get('middleware')
middleware() {
return RETURN_VALUE;
}
@Post('middleware')
noMiddleware() {
return RETURN_VALUE;
}
@Get('wildcard/overview')
testOverview() {
return RETURN_VALUE;
}
@Get('overview/:id')
overviewById() {
return RETURN_VALUE;
}
}
@Module({
imports: [ApplicationModule],
controllers: [TestController],
})
class TestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => res.end(MIDDLEWARE_VALUE))
.exclude('test', 'overview/:id', 'wildcard/(.*)', {
path: 'middleware',
method: RequestMethod.POST,
})
.forRoutes('*');
}
}
describe('Exclude middleware (fastify)', () => {
let app: INestApplication;
beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());
await app.init();
await app.getHttpAdapter().getInstance().ready();
});
it(`should exclude "/test" endpoint`, () => {
return request(app.getHttpServer()).get('/test').expect(200, RETURN_VALUE);
});
it(`should not exclude "/test2" endpoint`, () => {
return request(app.getHttpServer())
.get('/test2')
.expect(200, MIDDLEWARE_VALUE);
});
it(`should run middleware for "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.get('/middleware')
.expect(200, MIDDLEWARE_VALUE);
});
it(`should exclude POST "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.post('/middleware')
.expect(201, RETURN_VALUE);
});
it(`should exclude "/overview/:id" endpoint (by param)`, () => {
return request(app.getHttpServer())
.get('/overview/1')
.expect(200, RETURN_VALUE);
});
it(`should exclude "/wildcard/overview" endpoint (by wildcard)`, () => {
return request(app.getHttpServer())
.get('/wildcard/overview')
.expect(200, RETURN_VALUE);
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -1,116 +0,0 @@
import {
Controller,
Get,
INestApplication,
MiddlewareConsumer,
Module,
Post,
RequestMethod,
} from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
const RETURN_VALUE = 'test';
const MIDDLEWARE_VALUE = 'middleware';
@Controller()
class TestController {
@Get('test')
test() {
return RETURN_VALUE;
}
@Get('test2')
test2() {
return RETURN_VALUE;
}
@Get('middleware')
middleware() {
return RETURN_VALUE;
}
@Post('middleware')
noMiddleware() {
return RETURN_VALUE;
}
@Get('wildcard/overview')
testOverview() {
return RETURN_VALUE;
}
@Get('overview/:id')
overviewById() {
return RETURN_VALUE;
}
}
@Module({
imports: [ApplicationModule],
controllers: [TestController],
})
class TestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => res.send(MIDDLEWARE_VALUE))
.exclude('test', 'overview/:id', 'wildcard/(.*)', {
path: 'middleware',
method: RequestMethod.POST,
})
.forRoutes('*');
}
}
describe('Exclude middleware', () => {
let app: INestApplication;
beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication();
await app.init();
});
it(`should exclude "/test" endpoint`, () => {
return request(app.getHttpServer()).get('/test').expect(200, RETURN_VALUE);
});
it(`should not exclude "/test2" endpoint`, () => {
return request(app.getHttpServer())
.get('/test2')
.expect(200, MIDDLEWARE_VALUE);
});
it(`should run middleware for "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.get('/middleware')
.expect(200, MIDDLEWARE_VALUE);
});
it(`should exclude POST "/middleware" endpoint`, () => {
return request(app.getHttpServer())
.post('/middleware')
.expect(201, RETURN_VALUE);
});
it(`should exclude "/overview/:id" endpoint (by param)`, () => {
return request(app.getHttpServer())
.get('/overview/1')
.expect(200, RETURN_VALUE);
});
it(`should exclude "/wildcard/overview" endpoint (by wildcard)`, () => {
return request(app.getHttpServer())
.get('/wildcard/overview')
.expect(200, RETURN_VALUE);
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -20,7 +20,10 @@ describe('Hello world (express instance)', () => {
});
it(`/GET`, () => {
return request(server).get('/hello').expect(200).expect('Hello world!');
return request(server)
.get('/hello')
.expect(200)
.expect('Hello world!');
});
it(`/GET (Promise/async)`, () => {
@@ -37,14 +40,6 @@ describe('Hello world (express instance)', () => {
.expect('Hello world!');
});
it(`/GET { host: ":tenant.example.com" } not matched`, () => {
return request(server).get('/host').expect(404).expect({
statusCode: 404,
error: 'Not Found',
message: 'Cannot GET /host',
});
});
afterEach(async () => {
await app.close();
});

View File

@@ -1,73 +0,0 @@
import { INestApplication } from '@nestjs/common';
import { ExpressAdapter } from '@nestjs/platform-express';
import { Test } from '@nestjs/testing';
import * as express from 'express';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
describe('Hello world (express instance with multiple applications)', () => {
let server;
let apps: INestApplication[];
beforeEach(async () => {
const module1 = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
const module2 = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
const adapter = new ExpressAdapter(express());
apps = [
module1.createNestApplication(adapter),
module2.createNestApplication(adapter).setGlobalPrefix('/app2'),
];
await Promise.all(apps.map(app => app.init()));
server = adapter.getInstance();
});
it(`/GET`, () => {
return request(server).get('/hello').expect(200).expect('Hello world!');
});
it(`/GET (app2)`, () => {
return request(server)
.get('/app2/hello')
.expect(200)
.expect('Hello world!');
});
it(`/GET (Promise/async)`, () => {
return request(server)
.get('/hello/async')
.expect(200)
.expect('Hello world!');
});
it(`/GET (app2 Promise/async)`, () => {
return request(server)
.get('/app2/hello/async')
.expect(200)
.expect('Hello world!');
});
it(`/GET (Observable stream)`, () => {
return request(server)
.get('/hello/stream')
.expect(200)
.expect('Hello world!');
});
it(`/GET (app2 Observable stream)`, () => {
return request(server)
.get('/app2/hello/stream')
.expect(200)
.expect('Hello world!');
});
afterEach(async () => {
await Promise.all(apps.map(app => app.close()));
});
});

View File

@@ -1,21 +1,20 @@
import {
FastifyAdapter,
NestFastifyApplication,
INestFastifyApplication,
} from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';
import { fail } from 'assert';
import { expect } from 'chai';
import { ApplicationModule } from '../src/app.module';
describe('Hello world (fastify adapter)', () => {
let app: NestFastifyApplication;
let app: INestFastifyApplication;
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
app = module.createNestApplication<NestFastifyApplication>(
app = module.createNestApplication<INestFastifyApplication>(
new FastifyAdapter(),
);
await app.init();
@@ -48,27 +47,6 @@ describe('Hello world (fastify adapter)', () => {
.then(({ payload }) => expect(payload).to.be.eql('Hello world!'));
});
it(`/GET { host: ":tenant.example.com" } not matched`, () => {
return app
.inject({
method: 'GET',
url: '/host',
})
.then(
({ payload }) => {
fail(`Unexpected success: ${payload}`);
},
err => {
expect(err.getResponse()).to.be.eql({
error: 'Internal Server Error',
message:
'HTTP adapter does not support filtering on host: ":tenant.example.com"',
statusCode: 500,
});
},
);
});
afterEach(async () => {
await app.close();
});

View File

@@ -1,91 +0,0 @@
/* Temporarily disabled due to various regressions
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import { ApplicationModule } from '../src/app.module';
describe('Hello world (fastify adapter with multiple applications)', () => {
let adapter: FastifyAdapter;
let apps: NestFastifyApplication[];
beforeEach(async () => {
const module1 = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
const module2 = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
adapter = new FastifyAdapter();
apps = [
module1.createNestApplication<NestFastifyApplication>(adapter),
module2
.createNestApplication<NestFastifyApplication>(adapter, {
bodyParser: false,
})
.setGlobalPrefix('/app2'),
];
await Promise.all(apps.map(app => app.init()));
});
it(`/GET`, () => {
return adapter
.inject({
method: 'GET',
url: '/hello',
})
.then(({ payload }) => expect(payload).to.be.eql('Hello world!'));
});
it(`/GET (app2)`, () => {
return adapter
.inject({
method: 'GET',
url: '/app2/hello',
})
.then(({ payload }) => expect(payload).to.be.eql('Hello world!'));
});
it(`/GET (Promise/async)`, () => {
return adapter
.inject({
method: 'GET',
url: '/hello/async',
})
.then(({ payload }) => expect(payload).to.be.eql('Hello world!'));
});
it(`/GET (app2 Promise/async)`, () => {
return adapter
.inject({
method: 'GET',
url: '/app2/hello/async',
})
.then(({ payload }) => expect(payload).to.be.eql('Hello world!'));
});
it(`/GET (Observable stream)`, () => {
return adapter
.inject({
method: 'GET',
url: '/hello/stream',
})
.then(({ payload }) => expect(payload).to.be.eql('Hello world!'));
});
it(`/GET (app2 Observable stream)`, () => {
return adapter
.inject({
method: 'GET',
url: '/app2/hello/stream',
})
.then(({ payload }) => expect(payload).to.be.eql('Hello world!'));
});
afterEach(async () => {
await Promise.all(apps.map(app => app.close()));
await adapter.close();
});
});*/

View File

@@ -1,12 +1,8 @@
import {
INestApplication,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication, Injectable, UnauthorizedException } from '@nestjs/common';
import { ApplicationModule } from '../src/app.module';
import { APP_GUARD } from '@nestjs/core';
@Injectable()
export class AuthGuard {
@@ -34,9 +30,13 @@ describe('Guards', () => {
let app: INestApplication;
it(`should prevent access (unauthorized)`, async () => {
app = (await createTestModule(new AuthGuard())).createNestApplication();
app = (await createTestModule(
new AuthGuard(),
)).createNestApplication();
await app.init();
return request(app.getHttpServer()).get('/hello').expect(401);
return request(app.getHttpServer())
.get('/hello')
.expect(401);
});
});

View File

@@ -10,60 +10,42 @@ describe('Hello world (default adapter)', () => {
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
})
.compile();
app = module.createNestApplication();
server = app.getHttpServer();
await app.init();
});
[
{
host: 'example.com',
path: '/hello',
greeting: 'Hello world!',
},
{
host: 'acme.example.com',
path: '/host',
greeting: 'Host Greeting! tenant=acme',
},
].forEach(({ host, path, greeting }) => {
describe(`host=${host}`, () => {
describe('/GET', () => {
it(`should return "${greeting}"`, () => {
return request(server)
.get(path)
.set('Host', host)
.expect(200)
.expect(greeting);
});
it(`should attach response header`, () => {
return request(server)
.get(path)
.set('Host', host)
.expect(200)
.expect('Authorization', 'Bearer');
});
});
it(`/GET (Promise/async) returns "${greeting}"`, () => {
return request(server)
.get(`${path}/async`)
.set('Host', host)
.expect(200)
.expect(greeting);
});
it(`/GET (Observable stream) "${greeting}"`, () => {
return request(server)
.get(`${path}/stream`)
.set('Host', host)
.expect(200)
.expect(greeting);
});
describe('/GET', () => {
it(`should return "Hello world!"`, () => {
return request(server)
.get('/hello')
.expect(200)
.expect('Hello world!');
});
it(`should attach response header`, () => {
return request(server)
.get('/hello')
.expect(200)
.expect('Authorization', 'Bearer');
});
});
it(`/GET (Promise/async)`, () => {
return request(server)
.get('/hello/async')
.expect(200)
.expect('Hello world!');
});
it(`/GET (Observable stream)`, () => {
return request(server)
.get('/hello/stream')
.expect(200)
.expect('Hello world!');
});
afterEach(async () => {

View File

@@ -28,34 +28,6 @@ export class TransformInterceptor {
}
}
@Injectable()
export class StatusInterceptor {
constructor(private readonly statusCode: number) {}
intercept(context: ExecutionContext, next: CallHandler) {
const ctx = context.switchToHttp();
const res = ctx.getResponse();
res.status(this.statusCode);
return next.handle().pipe(map(data => ({ data })));
}
}
@Injectable()
export class HeaderInterceptor {
constructor(private readonly headers: object) {}
intercept(context: ExecutionContext, next: CallHandler) {
const ctx = context.switchToHttp();
const res = ctx.getResponse();
for (const key in this.headers) {
if (this.headers.hasOwnProperty(key)) {
res.header(key, this.headers[key]);
}
}
return next.handle().pipe(map(data => ({ data })));
}
}
function createTestModule(interceptor) {
return Test.createTestingModule({
imports: [ApplicationModule],
@@ -72,18 +44,20 @@ describe('Interceptors', () => {
let app: INestApplication;
it(`should transform response (sync)`, async () => {
app = (
await createTestModule(new OverrideInterceptor())
).createNestApplication();
app = (await createTestModule(
new OverrideInterceptor(),
)).createNestApplication();
await app.init();
return request(app.getHttpServer()).get('/hello').expect(200, RETURN_VALUE);
return request(app.getHttpServer())
.get('/hello')
.expect(200, RETURN_VALUE);
});
it(`should map response`, async () => {
app = (
await createTestModule(new TransformInterceptor())
).createNestApplication();
app = (await createTestModule(
new TransformInterceptor(),
)).createNestApplication();
await app.init();
return request(app.getHttpServer())
@@ -92,9 +66,9 @@ describe('Interceptors', () => {
});
it(`should map response (async)`, async () => {
app = (
await createTestModule(new TransformInterceptor())
).createNestApplication();
app = (await createTestModule(
new TransformInterceptor(),
)).createNestApplication();
await app.init();
return request(app.getHttpServer())
@@ -103,9 +77,9 @@ describe('Interceptors', () => {
});
it(`should map response (stream)`, async () => {
app = (
await createTestModule(new TransformInterceptor())
).createNestApplication();
app = (await createTestModule(
new TransformInterceptor(),
)).createNestApplication();
await app.init();
return request(app.getHttpServer())
@@ -113,33 +87,6 @@ describe('Interceptors', () => {
.expect(200, { data: 'Hello world!' });
});
it(`should modify response status`, async () => {
app = (
await createTestModule(new StatusInterceptor(400))
).createNestApplication();
await app.init();
return request(app.getHttpServer())
.get('/hello')
.expect(400, { data: 'Hello world!' });
});
it(`should modify Authorization header`, async () => {
const customHeaders = {
Authorization: 'jwt',
};
app = (
await createTestModule(new HeaderInterceptor(customHeaders))
).createNestApplication();
await app.init();
return request(app.getHttpServer())
.get('/hello')
.expect(200)
.expect('Authorization', 'jwt');
});
afterEach(async () => {
await app.close();
});

View File

@@ -1,6 +1,6 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { ApplicationModule } from '../src/app.module';
describe('Hello world (default adapter)', () => {
@@ -10,39 +10,23 @@ describe('Hello world (default adapter)', () => {
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [ApplicationModule],
}).compile();
})
.compile();
app = module.createNestApplication();
server = app.getHttpServer();
await app.init();
});
it(`host=example.com should execute locally injected pipe by HelloController`, () => {
return request(server).get('/hello/local-pipe/1').expect(200).expect({
id: '1',
});
});
it(`host=host.example.com should execute locally injected pipe by HostController`, () => {
it(`should execute locally injected pipe`, () => {
return request(server)
.get('/host/local-pipe/1')
.set('Host', 'acme.example.com')
.get('/hello/local-pipe/1')
.expect(200)
.expect({
id: '1',
host: true,
tenant: 'acme',
});
});
it(`should return 404 for mismatched host`, () => {
return request(server).get('/host/local-pipe/1').expect(404).expect({
error: 'Not Found',
message: 'Cannot GET /host/local-pipe/1',
statusCode: 404,
});
});
afterEach(async () => {
await app.close();
});

View File

@@ -1,60 +0,0 @@
import {
Controller,
Get,
INestApplication,
Injectable,
MiddlewareConsumer,
Module,
} from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
const RETURN_VALUE = 'test';
@Injectable()
class Middleware {
use(req, res, next) {
res.send(RETURN_VALUE);
}
}
@Controller()
class TestController {
@Get('test')
test() {
return 'test';
}
}
@Module({
imports: [ApplicationModule],
controllers: [TestController],
})
class TestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(Middleware).forRoutes('*');
}
}
describe('Middleware (class)', () => {
let app: INestApplication;
beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication();
await app.init();
});
it(`forRoutes(*)`, () => {
return request(app.getHttpServer()).get('/hello').expect(200, RETURN_VALUE);
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -1,61 +0,0 @@
import { INestApplication, MiddlewareConsumer, Module } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
const RETURN_VALUE_A = 'test_A';
const RETURN_VALUE_B = 'test_B';
@Module({
imports: [],
})
class ModuleA {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => {
res.send(RETURN_VALUE_A);
})
.forRoutes('hello');
}
}
@Module({
imports: [ModuleA],
})
class ModuleB {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => {
res.send(RETURN_VALUE_B);
})
.forRoutes('hello');
}
}
@Module({
imports: [ModuleB],
})
class TestModule {}
describe('Middleware (execution order)', () => {
let app: INestApplication;
beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication();
await app.init();
});
it(`should execute middleware in topological order`, () => {
return request(app.getHttpServer())
.get('/hello')
.expect(200, RETURN_VALUE_B);
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -1,87 +0,0 @@
import { Controller, Get, MiddlewareConsumer, Module } from '@nestjs/common';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import { ApplicationModule } from '../src/app.module';
const RETURN_VALUE = 'test';
const SCOPED_VALUE = 'test_scoped';
const WILDCARD_VALUE = 'test_wildcard';
@Controller()
class TestController {
@Get('test')
test() {
return RETURN_VALUE;
}
@Get('tests/wildcard_nested')
// eslint-disable-next-line @typescript-eslint/camelcase
wildcard_nested() {
return RETURN_VALUE;
}
}
@Module({
imports: [ApplicationModule],
controllers: [TestController],
})
class TestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => res.end(WILDCARD_VALUE))
.forRoutes('tests/(.*)')
.apply((req, res, next) => res.end(SCOPED_VALUE))
.forRoutes(TestController)
.apply((req, res, next) => res.end(RETURN_VALUE))
.forRoutes('(.*)');
}
}
describe('Middleware (FastifyAdapter)', () => {
let app: NestFastifyApplication;
beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());
await app.init();
});
it(`forRoutes(*)`, () => {
return app
.inject({
method: 'GET',
url: '/hello',
})
.then(({ payload }) => expect(payload).to.be.eql(RETURN_VALUE));
});
it(`forRoutes(TestController)`, () => {
return app
.inject({
method: 'GET',
url: '/test',
})
.then(({ payload }) => expect(payload).to.be.eql(SCOPED_VALUE));
});
it(`forRoutes(tests/*)`, () => {
return app
.inject({
method: 'GET',
url: '/tests/wildcard',
})
.then(({ payload }) => expect(payload).to.be.eql(WILDCARD_VALUE));
});
afterEach(async () => {
await app.close();
});
});

View File

@@ -1,76 +0,0 @@
import {
Controller,
Get,
INestApplication,
MiddlewareConsumer,
Module,
} from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
const RETURN_VALUE = 'test';
const SCOPED_VALUE = 'test_scoped';
const WILDCARD_VALUE = 'test_wildcard';
@Controller()
class TestController {
@Get('test')
test() {
return RETURN_VALUE;
}
@Get('tests/wildcard_nested')
// eslint-disable-next-line @typescript-eslint/camelcase
wildcard_nested() {
return RETURN_VALUE;
}
}
@Module({
imports: [ApplicationModule],
controllers: [TestController],
})
class TestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => res.send(WILDCARD_VALUE))
.forRoutes('tests/*')
.apply((req, res, next) => res.send(SCOPED_VALUE))
.forRoutes(TestController)
.apply((req, res, next) => res.send(RETURN_VALUE))
.forRoutes('*');
}
}
describe('Middleware', () => {
let app: INestApplication;
beforeEach(async () => {
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication();
await app.init();
});
it(`forRoutes(*)`, () => {
return request(app.getHttpServer()).get('/hello').expect(200, RETURN_VALUE);
});
it(`forRoutes(TestController)`, () => {
return request(app.getHttpServer()).get('/test').expect(200, SCOPED_VALUE);
});
it(`forRoutes(tests/*)`, () => {
return request(app.getHttpServer())
.get('/tests/wildcard')
.expect(200, WILDCARD_VALUE);
});
afterEach(async () => {
await app.close();
});
});

1775
integration/hello-world/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,27 @@
{
"name": "nest-typescript-starter",
"version": "1.0.0",
"description": "Nest TypeScript starter repository",
"license": "MIT",
"scripts": {
"start": "ts-node src/main"
},
"dependencies": {
"@nestjs/common": "^5.0.0",
"@nestjs/core": "^5.0.0",
"@nestjs/microservices": "^5.0.0",
"@nestjs/testing": "^5.0.0",
"@nestjs/websockets": "^5.0.0",
"class-transformer": "^0.1.7",
"class-validator": "^0.7.2",
"fastify": "^1.1.1",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"typescript": "^3.1.0"
},
"devDependencies": {
"@types/node": "^7.0.41",
"supertest": "^3.0.0",
"ts-node": "^6.0.0"
}
}

View File

@@ -1,8 +1,7 @@
import { Module } from '@nestjs/common';
import { HelloModule } from './hello/hello.module';
import { HostModule } from './host/host.module';
@Module({
imports: [HelloModule, HostModule],
imports: [HelloModule],
})
export class ApplicationModule {}

View File

@@ -1,6 +1,6 @@
import { HelloService } from './hello.service';
import { Controller, Get, Header, Param } from '@nestjs/common';
import { Observable, of } from 'rxjs';
import { HelloService } from './hello.service';
import { UserByIdPipe } from './users/user-by-id.pipe';
@Controller('hello')
@@ -15,7 +15,7 @@ export class HelloController {
@Get('async')
async asyncGreeting(): Promise<string> {
return this.helloService.greeting();
return await this.helloService.greeting();
}
@Get('stream')

View File

@@ -1,10 +0,0 @@
import { IsString, IsNotEmpty, IsNumber } from 'class-validator';
export class TestDto {
@IsString()
@IsNotEmpty()
string: string;
@IsNumber()
number: number;
}

View File

@@ -1,37 +0,0 @@
import { Controller, Get, Header, HostParam, Param } from '@nestjs/common';
import { Observable, of } from 'rxjs';
import { HostService } from './host.service';
import { UserByIdPipe } from './users/user-by-id.pipe';
@Controller({
path: 'host',
host: ':tenant.example.com',
})
export class HostController {
constructor(private readonly hostService: HostService) {}
@Get()
@Header('Authorization', 'Bearer')
greeting(@HostParam('tenant') tenant: string): string {
return `${this.hostService.greeting()} tenant=${tenant}`;
}
@Get('async')
async asyncGreeting(@HostParam('tenant') tenant: string): Promise<string> {
return `${await this.hostService.greeting()} tenant=${tenant}`;
}
@Get('stream')
streamGreeting(@HostParam('tenant') tenant: string): Observable<string> {
return of(`${this.hostService.greeting()} tenant=${tenant}`);
}
@Get('local-pipe/:id')
localPipe(
@Param('id', UserByIdPipe)
user: any,
@HostParam('tenant') tenant: string,
): any {
return { ...user, tenant };
}
}

View File

@@ -1,10 +0,0 @@
import { Module } from '@nestjs/common';
import { HostController } from './host.controller';
import { HostService } from './host.service';
import { UsersService } from './users/users.service';
@Module({
controllers: [HostController],
providers: [HostService, UsersService],
})
export class HostModule {}

View File

@@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class HostService {
greeting(): string {
return 'Host Greeting!';
}
}

View File

@@ -1,11 +0,0 @@
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common';
import { UsersService } from './users.service';
@Injectable()
export class UserByIdPipe implements PipeTransform<string> {
constructor(private readonly usersService: UsersService) {}
transform(value: string, metadata: ArgumentMetadata) {
return this.usersService.findById(value);
}
}

View File

@@ -1,8 +0,0 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class UsersService {
findById(id: string) {
return { id, host: true };
}
}

View File

@@ -0,0 +1,53 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {
"no-unused-expression": true
},
"rules": {
"eofline": false,
"quotemark": [
true,
"single"
],
"ordered-imports": [
false
],
"max-line-length": [
150
],
"member-ordering": [
false
],
"curly": false,
"interface-name": [
false
],
"array-type": [
false
],
"member-access": [
false
],
"no-empty-interface": false,
"no-empty": false,
"arrow-parens": false,
"object-literal-sort-keys": false,
"no-unused-expression": false,
"max-classes-per-file": [
false
],
"variable-name": [
false
],
"one-line": [
false
],
"one-variable-per-declaration": [
false
]
},
"rulesDirectory": []
}

View File

@@ -1,53 +0,0 @@
import { BeforeApplicationShutdown, Injectable } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as Sinon from 'sinon';
@Injectable()
class TestInjectable implements BeforeApplicationShutdown {
beforeApplicationShutdown = Sinon.spy();
}
describe('BeforeApplicationShutdown', () => {
it('should call `beforeApplicationShutdown` when application closes', async () => {
const module = await Test.createTestingModule({
providers: [TestInjectable],
}).compile();
const app = module.createNestApplication();
await app.close();
const instance = module.get(TestInjectable);
expect(instance.beforeApplicationShutdown.called).to.be.true;
});
/*
it('should not stop the server once beforeApplicationShutdown has been called', async () => {
let resolve;
const promise = new Promise(r => (resolve = r));
const module = await Test.createTestingModule({
providers: [
{
provide: 'Test',
useValue: {
beforeApplicationShutdown: () => promise,
},
},
],
}).compile();
Sinon.stub(module, 'dispose' as any);
const app = module.createNestApplication();
app.close();
expect(((module as any).dispose as Sinon.SinonSpy).called, 'dispose').to.be
.false;
resolve();
setTimeout(
() =>
expect(((module as any).dispose as Sinon.SinonSpy).called, 'dispose').to
.be.true,
0,
);
});*/
});

View File

@@ -1,54 +0,0 @@
import { expect } from 'chai';
import { spawnSync } from 'child_process';
import { join } from 'path';
describe('enableShutdownHooks', () => {
it('should call the correct hooks if any shutdown signal gets invoked', done => {
const result = spawnSync('ts-node', [
join(__dirname, '../src/enable-shutdown-hooks-main.ts'),
'SIGHUP',
]);
const calls = result.stdout
.toString()
.split('\n')
.map((call: string) => call.trim());
expect(calls[0]).to.equal('beforeApplicationShutdown SIGHUP');
expect(calls[1]).to.equal('onApplicationShutdown SIGHUP');
done();
}).timeout(5000);
it('should call the correct hooks if a specific shutdown signal gets invoked', done => {
const result = spawnSync('ts-node', [
join(__dirname, '../src/enable-shutdown-hooks-main.ts'),
'SIGINT',
'SIGINT',
]);
const calls = result.stdout
.toString()
.split('\n')
.map((call: string) => call.trim());
expect(calls[0]).to.equal('beforeApplicationShutdown SIGINT');
expect(calls[1]).to.equal('onApplicationShutdown SIGINT');
done();
}).timeout(5000);
it('should ignore system signals which are not specified', done => {
const result = spawnSync('ts-node', [
join(__dirname, '../src/enable-shutdown-hooks-main.ts'),
'SIGINT',
'SIGHUP',
]);
expect(result.stdout.toString().trim()).to.be.eq('');
done();
}).timeout(5000);
it('should ignore system signals if "enableShutdownHooks" was not called', done => {
const result = spawnSync('ts-node', [
join(__dirname, '../src/enable-shutdown-hooks-main.ts'),
'SIGINT',
'NONE',
]);
expect(result.stdout.toString().trim()).to.be.eq('');
done();
}).timeout(5000);
});

View File

@@ -1,46 +0,0 @@
import { Test } from '@nestjs/testing';
import * as Sinon from 'sinon';
import {
Injectable,
OnApplicationBootstrap,
OnApplicationShutdown,
OnModuleDestroy,
OnModuleInit,
BeforeApplicationShutdown,
} from '@nestjs/common';
@Injectable()
class TestInjectable
implements
OnApplicationBootstrap,
OnModuleInit,
OnModuleDestroy,
OnApplicationShutdown,
BeforeApplicationShutdown {
onApplicationBootstrap = Sinon.spy();
beforeApplicationShutdown = Sinon.spy();
onApplicationShutdown = Sinon.spy();
onModuleDestroy = Sinon.spy();
onModuleInit = Sinon.spy();
}
describe('Lifecycle Hook Order', () => {
it('should call the lifecycle hooks in the correct order', async () => {
const module = await Test.createTestingModule({
providers: [TestInjectable],
}).compile();
const app = module.createNestApplication();
await app.init();
await app.close();
const instance = module.get(TestInjectable);
Sinon.assert.callOrder(
instance.onModuleInit,
instance.onApplicationBootstrap,
instance.onModuleDestroy,
instance.beforeApplicationShutdown,
instance.onApplicationShutdown,
);
});
});

View File

@@ -1,44 +0,0 @@
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as Sinon from 'sinon';
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
@Injectable()
class TestInjectable implements OnApplicationBootstrap {
onApplicationBootstrap = Sinon.spy();
}
describe('OnApplicationBootstrap', () => {
it('should call onApplicationBootstrap when application starts', async () => {
const module = await Test.createTestingModule({
providers: [TestInjectable],
}).compile();
const app = module.createNestApplication();
await app.init();
const instance = module.get(TestInjectable);
expect(instance.onApplicationBootstrap.called).to.be.true;
});
it('should not throw an error when onApplicationBootstrap is null', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onApplicationBootstrap: null } },
],
}).compile();
const app = module.createNestApplication();
await app.init().then(obj => expect(obj).to.not.be.undefined);
});
it('should not throw an error when onApplicationBootstrap is undefined', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onApplicationBootstrap: undefined } },
],
}).compile();
const app = module.createNestApplication();
await app.init().then(obj => expect(obj).to.not.be.undefined);
});
});

View File

@@ -1,22 +0,0 @@
import { Injectable, OnApplicationShutdown } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as Sinon from 'sinon';
@Injectable()
class TestInjectable implements OnApplicationShutdown {
onApplicationShutdown = Sinon.spy();
}
describe('OnApplicationShutdown', () => {
it('should call onApplicationShutdown when application closes', async () => {
const module = await Test.createTestingModule({
providers: [TestInjectable],
}).compile();
const app = module.createNestApplication();
await app.close();
const instance = module.get(TestInjectable);
expect(instance.onApplicationShutdown.called).to.be.true;
});
});

View File

@@ -1,42 +0,0 @@
import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as Sinon from 'sinon';
@Injectable()
class TestInjectable implements OnModuleDestroy {
onModuleDestroy = Sinon.spy();
}
describe('OnModuleDestroy', () => {
it('should call onModuleDestroy when application closes', async () => {
const module = await Test.createTestingModule({
providers: [TestInjectable],
}).compile();
const app = module.createNestApplication();
await app.close();
const instance = module.get(TestInjectable);
expect(instance.onModuleDestroy.called).to.be.true;
});
it('should not throw an error when onModuleDestroy is null', async () => {
const module = await Test.createTestingModule({
providers: [{ provide: 'TEST', useValue: { onModuleDestroy: null } }],
}).compile();
const app = module.createNestApplication();
await app.init().then(obj => expect(obj).to.not.be.undefined);
});
it('should not throw an error when onModuleDestroy is undefined', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: 'TEST', useValue: { onModuleDestroy: undefined } },
],
}).compile();
const app = module.createNestApplication();
await app.init().then(obj => expect(obj).to.not.be.undefined);
});
});

View File

@@ -1,40 +0,0 @@
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as Sinon from 'sinon';
import { Injectable, OnModuleInit } from '@nestjs/common';
@Injectable()
class TestInjectable implements OnModuleInit {
onModuleInit = Sinon.spy();
}
describe('OnModuleInit', () => {
it('should call onModuleInit when application starts', async () => {
const module = await Test.createTestingModule({
providers: [TestInjectable],
}).compile();
const app = module.createNestApplication();
await app.init();
const instance = module.get(TestInjectable);
expect(instance.onModuleInit.called).to.be.true;
});
it('should not throw an error when onModuleInit is null', async () => {
const module = await Test.createTestingModule({
providers: [{ provide: 'TEST', useValue: { onModuleInit: null } }],
}).compile();
const app = module.createNestApplication();
await app.init().then(obj => expect(obj).to.not.be.undefined);
});
it('should not throw an error when onModuleInit is undefined', async () => {
const module = await Test.createTestingModule({
providers: [{ provide: 'TEST', useValue: { onModuleInit: undefined } }],
}).compile();
const app = module.createNestApplication();
await app.init().then(obj => expect(obj).to.not.be.undefined);
});
});

View File

@@ -1,41 +0,0 @@
import {
BeforeApplicationShutdown,
Injectable,
Module,
OnApplicationShutdown,
} from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
const SIGNAL = process.argv[2];
const SIGNAL_TO_LISTEN = process.argv[3];
@Injectable()
class TestInjectable
implements OnApplicationShutdown, BeforeApplicationShutdown {
beforeApplicationShutdown(signal: string) {
console.log('beforeApplicationShutdown ' + signal);
}
onApplicationShutdown(signal: string) {
console.log('onApplicationShutdown ' + signal);
}
}
@Module({
providers: [TestInjectable],
})
class AppModule {}
async function bootstrap() {
const app = await NestFactory.create(AppModule, { logger: true });
if (SIGNAL_TO_LISTEN && SIGNAL_TO_LISTEN !== 'NONE') {
app.enableShutdownHooks([SIGNAL_TO_LISTEN]);
} else if (SIGNAL_TO_LISTEN !== 'NONE') {
app.enableShutdownHooks();
}
await app.listen(1800);
process.kill(process.pid, SIGNAL);
}
bootstrap();

Some files were not shown because too many files have changed in this diff Show More