diff --git a/.circleci/config.yml b/.circleci/config.yml index 324f306..3aa5f98 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,98 +1,110 @@ version: 2.1 references: - x-workdir: &work-dir - working_directory: ~/source + x-workdir: &work-dir + working_directory: ~/source - x-save-workspace: &persist-step - persist_to_workspace: - root: ~/source - paths: - - . + x-save-workspace: &persist-step + persist_to_workspace: + root: ~/source + paths: + - . - x-attach: &attach-step - attach_workspace: - at: . + x-attach: &attach-step + attach_workspace: + at: . jobs: - install-dependencies: - <<: *work-dir - docker: - - image: circleci/node:10 - environment: - HUSKY_SKIP_INSTALL: 1 - steps: - - checkout - - restore_cache: - keys: - - v1-npm-{{ checksum "package-lock.json" }} - - run: - name: Install dependencies - command: | - if [ ! -d node_modules ]; then - npm ci - fi - - save_cache: - key: v1-npm-{{ checksum "package-lock.json" }} - paths: - - node_modules - - *persist-step + install-dependencies: + <<: *work-dir + docker: + - image: circleci/node:10 + environment: + HUSKY_SKIP_INSTALL: 1 + steps: + - checkout + - restore_cache: + keys: + - v1-npm-{{ checksum "package-lock.json" }} + - run: + name: Install dependencies + command: | + if [ ! -d node_modules ]; then + npm ci + fi + - save_cache: + key: v1-npm-{{ checksum "package-lock.json" }} + paths: + - node_modules + - *persist-step - lint: - <<: *work-dir - docker: - - image: node - steps: - - *attach-step - - run: - name: lint - command: npm run lint + lint: + <<: *work-dir + docker: + - image: node + steps: + - *attach-step + - run: + name: lint + command: npm run lint - node-10: - docker: - - image: circleci/node:10 - steps: - - *attach-step - - run: - name: Test - command: npm test + prettier: + <<: *work-dir + docker: + - image: node + steps: + - *attach-step + - run: + name: prettier + command: npm run prettier:check - node-12: - docker: - - image: circleci/node:12 - steps: - - *attach-step - - run: - name: Test with coverage - command: npm run test:coverage - - run: - name: Upload coverage report - command: bash <(curl -s https://codecov.io/bash) -F unit -s coverage/lcov.info + node-10: + docker: + - image: circleci/node:10 + steps: + - *attach-step + - run: + name: Test + command: npm test - node-14: - docker: - - image: circleci/node:14 - steps: - - *attach-step - - run: - name: Test - command: npm test + node-12: + docker: + - image: circleci/node:12 + steps: + - *attach-step + - run: + name: Test with coverage + command: npm run test:coverage + - run: + name: Upload coverage report + command: bash <(curl -s https://codecov.io/bash) -F unit -s coverage/lcov.info + node-14: + docker: + - image: circleci/node:14 + steps: + - *attach-step + - run: + name: Test + command: npm test workflows: - version: 2 - nise: - jobs: - - install-dependencies - - lint: - requires: - - install-dependencies - - node-10: - requires: - - install-dependencies - - node-12: - requires: - - install-dependencies - - node-14: - requires: + version: 2 + nise: + jobs: - install-dependencies + - lint: + requires: + - install-dependencies + - prettier: + requires: + - install-dependencies + - node-10: + requires: + - install-dependencies + - node-12: + requires: + - install-dependencies + - node-14: + requires: + - install-dependencies diff --git a/.eslintrc.yml b/.eslintrc.yml index fc6ae4a..7c4ad5a 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -1,31 +1,31 @@ extends: eslint-config-sinon globals: - ArrayBuffer: false - Uint8Array: false + ArrayBuffer: false + Uint8Array: false plugins: - - ie11 + - ie11 rules: - ie11/no-collection-args: error - ie11/no-for-in-const: error - ie11/no-loop-func: warn - ie11/no-weak-collections: error + ie11/no-collection-args: error + ie11/no-for-in-const: error + ie11/no-loop-func: warn + ie11/no-weak-collections: error + prettier/prettier: off overrides: - - - files: '*.test.*' - env: - mocha: true + - files: "*.test.*" + env: + mocha: true - plugins: - - mocha + plugins: + - mocha - rules: - mocha/no-exclusive-tests: error - mocha/no-identical-title: error - mocha/no-nested-tests: error - mocha/no-sibling-hooks: error - mocha/no-top-level-hooks: error - max-nested-callbacks: off + rules: + mocha/no-exclusive-tests: error + mocha/no-identical-title: error + mocha/no-nested-tests: error + mocha/no-sibling-hooks: error + mocha/no-top-level-hooks: error + max-nested-callbacks: off diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..ef819c0 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,5 @@ +nise.js +coverage/ +site/ +.nyc_output/ +History.md diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..47c9065 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,2 @@ +# This project uses Prettier defaults, the config file is present in +# order to trigger editor plugins to allow format on save behaviour diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 6e092c4..fe66746 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -10,22 +10,22 @@ We pledge to act and interact in ways that contribute to an open, welcoming, div Examples of behavior that contributes to a positive environment for our community include: -* Demonstrating empathy and kindness toward other people -* Being respectful of differing opinions, viewpoints, and experiences -* Giving and gracefully accepting constructive feedback -* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience -* Focusing on what is best not just for us as individuals, but for the overall community +- Demonstrating empathy and kindness toward other people +- Being respectful of differing opinions, viewpoints, and experiences +- Giving and gracefully accepting constructive feedback +- Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience +- Focusing on what is best not just for us as individuals, but for the overall community Examples of unacceptable behavior include: -* The use of sexualized language or imagery, and sexual attention or - advances of any kind -* Trolling, insulting or derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or email - address, without their explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting +- The use of sexualized language or imagery, and sexual attention or + advances of any kind +- Trolling, insulting or derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or email + address, without their explicit permission +- Other conduct which could reasonably be considered inappropriate in a + professional setting ## Enforcement Responsibilities @@ -67,7 +67,7 @@ Community leaders will follow these Community Impact Guidelines in determining t ### 4. Permanent Ban -**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. +**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. **Consequence**: A permanent ban from any sort of public interaction within the community. diff --git a/README.md b/README.md index d88a6db..51f733b 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ Support us with a monthly donation and help us continue our activities. [[Become - ## Sponsors Become a sponsor and get your logo on our README on GitHub with a link to your site. [[Become a sponsor](https://opencollective.com/sinon#sponsor)] diff --git a/docs/index.md b/docs/index.md index ec91ff4..8527410 100644 --- a/docs/index.md +++ b/docs/index.md @@ -41,7 +41,6 @@ var sinon = require("sinon"); } ``` - ### `useFakeXMLHttpRequest` #### var xhr = fakeXhr.useFakeXMLHttpRequest(); @@ -52,7 +51,6 @@ In browsers that support `ActiveXObject`, this constructor is replaced, and fake The native `XMLHttpRequest` object will be available at `fakeXhr.xhr.XMLHttpRequest` - #### `xhr.onCreate = function (xhr) {};` By assigning a function to the `onCreate` property of the returned object from `useFakeXMLHttpRequest()` you can subscribe to newly created `FakeXMLHttpRequest` objects. See below for the fake xhr object API. @@ -63,19 +61,16 @@ Using this observer means you can still reach objects created by e.g. `jQuery.aj Restore original function(s). - ### `FakeXMLHttpRequest` #### `String request.url` The URL set on the request object. - #### `String request.method` The request method as a string. - #### `Object request.requestHeaders` An object of all request headers, i.e.: @@ -87,7 +82,6 @@ An object of all request headers, i.e.: } ``` - #### `String request.requestBody` The request body @@ -98,61 +92,50 @@ The request's status code. `undefined` if the request has not been handled (see [`respond`](#respond) below) - #### `String request.statusText` Only populated if the [`respond`](#respond) method is called (see below). - #### `boolean request.async` Whether or not the request is asynchronous. - #### `String request.username` Username, if any. - #### `String request.password` Password, if any. - #### `Document request.responseXML` When using [`respond`](#respond), this property is populated with a parsed document if response headers indicate as much (see [the spec](http://www.w3.org/TR/XMLHttpRequest/)) - #### `String request.getResponseHeader(header);` The value of the given response header, if the request has been responded to (see [`respond`](#respond)). - #### `Object request.getAllResponseHeaders();` All response headers as an object. - ### Filtered requests When using Sinon.JS for mockups or partial integration/functional testing, you might want to fake some requests, while allowing others to go through to the backend server. With filtered `FakeXMLHttpRequest`s (new in v1.3.0), you can. - #### `FakeXMLHttpRequest.useFilters` Default `false`. When set to `true`, Sinon will check added filters if certain requests should be "unfaked" - #### `FakeXMLHttpRequest.addFilter(fn)` Add a filter that will decide whether or not to fake a request. The filter will be called when `xhr.open` is called, with the exact same arguments (`method`, `url`, `async`, `username`, `password`). If the filter returns `true`, the request will not be faked. - ### Simulating server responses #### `request.setStatus(status);` @@ -165,14 +148,12 @@ Status should be a number, the status text is looked up from `fakeXhr.FakeXMLHtt Sets response headers (e.g. `{ "Content-Type": "text/html", /* ... */ }`, updates the `readyState` property and fires `onreadystatechange`. - #### `request.setResponseBody(body);` Sets the respond body, updates the `readyState` property and fires `onreadystatechange`. Additionally, populates `responseXML` with a parsed document if [response headers indicate as much](http://www.w3.org/TR/XMLHttpRequest/). - #### `request.respond(status, headers, body);` Calls the above three methods. @@ -193,8 +174,8 @@ Note that this feature is intended to help during mockup development, and is not When `autoRespond` is `true`, respond to requests after this number of milliseconds. Default is 10. - ## Fake server + High-level API to manipulate `FakeXMLHttpRequest` instances. For help with handling JSON-P please refer to our [notes below](#json-p) @@ -228,7 +209,6 @@ var sinon = require("sinon"); } ``` - #### `var server = fakeServer.create([config]);` Creates a new server. @@ -237,14 +217,12 @@ This function also calls `useFakeXMLHttpRequest()`. `create` accepts optional properties to configure the fake server. See [options](#options) below for configuration parameters. - #### `var server = fakeServerWithClock.create();` Creates a server that also manages fake timers. This is useful when testing `XHR` objects created with e.g. jQuery 1.3.x, which uses a timer to poll the object for completion, rather than the usual `onreadystatechange`. - #### `server.configure(config);` Configures the fake server. @@ -267,19 +245,16 @@ When the response is a `Function`, it will be passed the request object. You must manually call [respond](#respond) on it to complete the request. - #### `server.respondWith(url, response);` Responds to all requests to given URL, e.g. `/posts/1`. - #### `server.respondWith(method, url, response);` Responds to all `method` requests to the given URL with the given response. `method` is an HTTP verb. - #### `server.respondWith(urlRegExp, response);` URL may be a regular expression, e.g. `/\\/post\\//\\d+` @@ -287,12 +262,15 @@ URL may be a regular expression, e.g. `/\\/post\\//\\d+` If the response is a `Function`, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object: ```js -server.respondWith(/\/todo-items\/(\d+)/, function (xhr, id) { - xhr.respond(200, { "Content-Type": "application/json" }, '[{ "id": ' + id + " }]"); +server.respondWith(/\/todo-items\/(\d+)/, function(xhr, id) { + xhr.respond( + 200, + { "Content-Type": "application/json" }, + '[{ "id": ' + id + " }]" + ); }); ``` - #### `server.respondWith(method, urlRegExp, response);` Responds to all `method` requests to URLs matching the regular expression. @@ -307,7 +285,6 @@ Synchronous requests are responded to immediately, so make sure to call `respond If called with arguments, `respondWith` will be called with those arguments before responding to requests. - #### `server.autoRespond = true;` If set, will automatically respond to every request after a timeout. @@ -316,7 +293,6 @@ The default timeout is 10ms but you can control it through the `autoRespondAfter Note that this feature is intended to help during mockup development, and is not suitable for use in tests. For synchronous immediate responses, use `respondImmediately` instead. - #### `server.autoRespondAfter = ms;` Causes the server to automatically respond to incoming requests after a timeout. @@ -340,7 +316,6 @@ If set to `true`, server will find `_method` parameter in POST body and recogniz Supports a pattern common to Ruby on Rails applications. For custom HTTP method faking, override `server.getHTTPMethod(request)`. - #### `server.getHTTPMethod(request)` Used internally to determine the HTTP method used with the provided request. @@ -349,19 +324,16 @@ By default this method simply returns `request.method`. When `server.fakeHTTPMet This method can be overridden to provide custom behavior. - #### `server.restore();` Restores the native XHR constructor. - ### Fake server options These options are properties on the server object and can be set directly - ```js -server.autoRespond = true +server.autoRespond = true; ``` You can also pass options with an object literal to `fakeServer.create` and `.configure`. @@ -376,7 +348,6 @@ Note that this feature is intended to help during mockup development, and is not For synchronous immediate responses, use `respondImmediately` instead. - #### `Number autoRespondAfter (ms)` Causes the server to automatically respond to incoming requests after a timeout. @@ -389,7 +360,6 @@ This is ideal for faking the server from within a test without having to call `s As this is synchronous and immediate, this is not suitable for simulating actual network latency in tests or mockups. To simulate network latency with automatic responses, see `server.autoRespond` and `server.autoRespondAfter`. - #### `boolean fakeHTTPMethods` If set to `true`, server will find `_method` parameter in `POST` body and recognize that as the actual method. diff --git a/package.json b/package.json index f3fb79b..a5e9e18 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,8 @@ "scripts": { "bundle": "browserify --no-detect-globals -s nise -o nise.js lib/index.js", "lint": "eslint .", + "prettier:check": "prettier --check '**/*.{js,css,md}'", + "prettier:write": "prettier --write '**/*.{js,css,md}'", "prepublish": "npm run bundle", "prepublishOnly": "mkdocs gh-deploy -r upstream || mkdocs gh-deploy -r origin", "test": "mocha lib/**/*.test.js", @@ -72,6 +74,7 @@ "path-to-regexp": "^1.7.0" }, "lint-staged": { + "*.{js,css,md}": "prettier --check", "*.js": "eslint" }, "husky": {