簡體   English   中英

為什么在玩笑中需要`module.exports`? 我該如何避免呢?

[英]Why do I need `module.exports` in jest? How can I avoid it?

嘗試使用jest框架測試我的react js代碼時遇到了麻煩。

可以說這是我的組成部分:

# coffee/global_widget.coffee
@GlobalWidget = React.createClass
  render: ->
    <div className='row'>
      <div className='col-md-12'>
        <TerminalWidget />
      </div>
    </div>

# coffee/terminal_widget.coffee
@TerminalWidget = React.createClass
  render: ->
    <div>Hey! This is the terminal!</div>

因此,我想使用jest進行測試。

jest.dontMock '../coffee/global_widget'

describe 'GlobalWidget', ->
  global.React = require('react/addons')
  GlobalWidget = require('../coffee/global_widget')
  TestUtils = React.addons.TestUtils

  globalWidgetForTest = TestUtils.renderIntoDocument(<GlobalWidget />)
  # body of the test

我有麻煩:

npm test

> terminal-ui@0.0.2 test /home/alex/my_project
> jest

Using Jest CLI v0.4.5
 FAIL  __tests__/global_widget-test.coffee (0.276s)
● GlobalWidget › it encountered a declaration exception
  - ReferenceError: GlobalWidget is not defined

如果我將module.exports = @GlobalWidget附加到coffee/global_widget.coffee ,那么我得到的TerminalWidget is not defined 什么是module.exports= ?為什么我需要在代碼中將其放入每個組件?

看起來jest無法訪問您的全局變量,因此您需要導出每個文件,並在需要時需要它們。
您需要使用module.exports,因為您在這行GlobalWidget = require('../coffee/global_widget')使用了requirejs語法。 這樣做是隔離您的代碼,以防止擁有一堆全局可用的代碼。 這樣,您僅導入(使用require )和導出(使用module.exports = ... )僅實際需要的代碼。 此外,在這種情況下,它允許諸如笑話之類的進程訪問其他可用文件。

如果您絕對不想使用requirejs (建議使用),則可以嘗試將它們添加到您的全局變量中,或者擺弄jests config使其在測試套件之前可用,盡管這可能比僅導出您的軟件包更困難。模組

如何修復“無法分配給 object 的只讀屬性“導出”'#<object> ' 將第二個 function 添加到 module.exports 時?<div id="text_translate"><p> 不幸的是,我已經被困了大約 5 天了。 我用谷歌搜索過,沒有什么對我有用。 我有一個 utils.js 文件,里面有很多東西可以幫助我。 當我將 function "doesUserContainRoles" 添加到導出時,我的 Chrome 控制台給了我錯誤:</p><blockquote><p> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'</p></blockquote><p> 當我刪除 function 時,utils 文件中的所有內容都可以完美運行,根本沒有 0 個問題。 只要我添加 function,我就會收到此錯誤。 我通過以下方式要求 utils 文件:</p><pre class="lang-js prettyprint-override"> const utils = require("../../../utils");</pre><p> 並且沒有與此一起使用的import 。 (我知道module.exports和import不能一起工作)</p><p> 我正在使用什么:</p><ul><li><p> Vue.js @vue/cli 4.5.8</p></li><li><p> Node.js v15.2.0</p></li></ul><h3> 文件:</h3><p> utils.js</p><pre class="lang-js prettyprint-override"> const winston = require("winston"); const { datadog } = require("./credentials.js"); const { createLogger, format, transports } = require('winston'); const base_url = "http://localhost:3001/api/v1" // Winston ~ const httpTransportOptions = { host: datadog.logger.host, path: datadog.logger.path, ssl: datadog.logger.ssl }; const customLevels = { levels: { emergency: 0, alert: 1, critical: 2, error: 3, warn: 4, notice: 5, info: 6, debug: 7, success: 8 }, } const logger = createLogger({ level: 'info', levels: customLevels.levels, exitOnError: false, format: format.json(), transports: [ new transports.Http(httpTransportOptions), ], }); const commonMessages = { accessPage: "User Accessed a Page", restrictedPage: "Attempt at accessing restricted page" } function alertGeneral() { alert("Oops. An error has occurred. Please try again later, If this problem continues. please contact Vinniehat;"), } function doesUserContainRoles(userRoles. containsRoles) { return.;userRoles.some(role =&gt; containsRoles,includes(role)), } module,exports = { logger, commonMessages, base_url, alertGeneral, doesUserContainRoles }</pre><p> 這是錯誤堆棧:</p><pre> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;' at Module.eval (utils.js?1b23:45) at eval (utils.js:68) at Module../utils.js (app.js:1944) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (router.js?9883:19) at Module../src/router/router.js (app.js:1812) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (main.js:34)</pre><p> 我也嘗試使用export default 。 這樣做時,我的前端工作。 網站加載沒有錯誤。 不過,我的后端使用 Express.js 運行,然后引發錯誤:</p><pre> Unhandled rejection E:\Development\Websites\iceberg-gaming-website\utils.js:45 export default { ^^^^^^ SyntaxError: Unexpected token 'export' at wrapSafe (node:internal/modules/cjs/loader:1018:16) at Module._compile (node:internal/modules/cjs/loader:1066:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at Object.&lt;anonymous&gt; (E:\Development\Websites\iceberg-gaming-website\express\modules\user.js:9:15) at Module._compile (node:internal/modules/cjs/loader:1102:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at E:\Development\Websites\iceberg-gaming-website\express\express.js:27:27 at tryCatcher (E:\Development\Websites\iceberg-gaming-website\node_modules\bluebird\js\release\util.js:16:23)</pre></div></object>

[英]How do I fix 'Cannot assign to read only property 'exports' of object '#<Object>' when adding a 2nd function to module.exports?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何通過 module.exports 導出這個名為 function 的文件? 如何在節點 js 中使用 module.exports 導出數組? 嘗試將中間件功能導出到module.exports對象時,為什么會得到“下一個不是功能”的信息? 回復:如何將javascript AMD模塊導入外部TypeScript模塊? (使用module.exports =…時) 我沒有從module.exports得到答案 如何在我的 index.js / discord.js 中傳遞 module.exports 如何修復“無法分配給 object 的只讀屬性“導出”'#<object> ' 將第二個 function 添加到 module.exports 時?<div id="text_translate"><p> 不幸的是,我已經被困了大約 5 天了。 我用谷歌搜索過,沒有什么對我有用。 我有一個 utils.js 文件,里面有很多東西可以幫助我。 當我將 function "doesUserContainRoles" 添加到導出時,我的 Chrome 控制台給了我錯誤:</p><blockquote><p> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;'</p></blockquote><p> 當我刪除 function 時,utils 文件中的所有內容都可以完美運行,根本沒有 0 個問題。 只要我添加 function,我就會收到此錯誤。 我通過以下方式要求 utils 文件:</p><pre class="lang-js prettyprint-override"> const utils = require("../../../utils");</pre><p> 並且沒有與此一起使用的import 。 (我知道module.exports和import不能一起工作)</p><p> 我正在使用什么:</p><ul><li><p> Vue.js @vue/cli 4.5.8</p></li><li><p> Node.js v15.2.0</p></li></ul><h3> 文件:</h3><p> utils.js</p><pre class="lang-js prettyprint-override"> const winston = require("winston"); const { datadog } = require("./credentials.js"); const { createLogger, format, transports } = require('winston'); const base_url = "http://localhost:3001/api/v1" // Winston ~ const httpTransportOptions = { host: datadog.logger.host, path: datadog.logger.path, ssl: datadog.logger.ssl }; const customLevels = { levels: { emergency: 0, alert: 1, critical: 2, error: 3, warn: 4, notice: 5, info: 6, debug: 7, success: 8 }, } const logger = createLogger({ level: 'info', levels: customLevels.levels, exitOnError: false, format: format.json(), transports: [ new transports.Http(httpTransportOptions), ], }); const commonMessages = { accessPage: "User Accessed a Page", restrictedPage: "Attempt at accessing restricted page" } function alertGeneral() { alert("Oops. An error has occurred. Please try again later, If this problem continues. please contact Vinniehat;"), } function doesUserContainRoles(userRoles. containsRoles) { return.;userRoles.some(role =&gt; containsRoles,includes(role)), } module,exports = { logger, commonMessages, base_url, alertGeneral, doesUserContainRoles }</pre><p> 這是錯誤堆棧:</p><pre> Uncaught TypeError: Cannot assign to read only property 'exports' of object '#&lt;Object&gt;' at Module.eval (utils.js?1b23:45) at eval (utils.js:68) at Module../utils.js (app.js:1944) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (router.js?9883:19) at Module../src/router/router.js (app.js:1812) at __webpack_require__ (app.js:854) at fn (app.js:151) at eval (main.js:34)</pre><p> 我也嘗試使用export default 。 這樣做時,我的前端工作。 網站加載沒有錯誤。 不過,我的后端使用 Express.js 運行,然后引發錯誤:</p><pre> Unhandled rejection E:\Development\Websites\iceberg-gaming-website\utils.js:45 export default { ^^^^^^ SyntaxError: Unexpected token 'export' at wrapSafe (node:internal/modules/cjs/loader:1018:16) at Module._compile (node:internal/modules/cjs/loader:1066:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at Object.&lt;anonymous&gt; (E:\Development\Websites\iceberg-gaming-website\express\modules\user.js:9:15) at Module._compile (node:internal/modules/cjs/loader:1102:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10) at Module.load (node:internal/modules/cjs/loader:967:32) at Function.Module._load (node:internal/modules/cjs/loader:807:14) at Module.require (node:internal/modules/cjs/loader:991:19) at require (node:internal/modules/cjs/helpers:92:18) at E:\Development\Websites\iceberg-gaming-website\express\express.js:27:27 at tryCatcher (E:\Development\Websites\iceberg-gaming-website\node_modules\bluebird\js\release\util.js:16:23)</pre></div></object> 如何在 node.js 的`module.exports = class` 中創建靜態變量 如何在設置module.exports之前等待異步功能完成 如何使 typescript 導出與 module.exports 向后兼容
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM