是否可以使用webpack定义一个全局变量,结果如下:
var myvar = {};
Run Code Online (Sandbox Code Playgroud)
我看到的所有示例都使用外部文件 require("imports?$=jquery!./file.js")
我有一个 Yarn monorepo(工作区),有 2 个包:后端(Node.js/TypeScript)和前端(React/Typescript)。
\n/package.json(已修剪)
{\n "workspaces": [\n "backend",\n "frontend"\n ],\n}\nRun Code Online (Sandbox Code Playgroud)\n我正在尝试添加与 GitHub Actions 的持续集成,并尝试使用它actions/cache@v2来缓存 Yarn 缓存目录和所有项目'node_modules目录
.github/workflows/CI.yml(已修剪)
steps:\n - uses: actions/checkout@v2\n\n - name: Get yarn cache directory path\n id: yarn-cache-dir-path\n run: |\n echo "::set-output name=dir::$(yarn cache dir)"\n echo "::set-output name=version::$(yarn -v)"\n\n - name: Use Node.js ${{ matrix.node-version }}\n uses: actions/setup-node@v2\n with:\n node-version: ${{ matrix.node-version }}\n\n - uses: actions/cache@v2\n with:\n path: |\n ${{ steps.yarn-cache-dir-path.outputs.dir }}\n '**/node_modules'\n '**/.eslintcache'\n key: …Run Code Online (Sandbox Code Playgroud) 我想知道如何在javascript中减去两个负浮点数.我试过了:
alert(-0.2-0.1);
Run Code Online (Sandbox Code Playgroud)
结果是-0.30000000000000004.难道我做错了什么?我该怎么做-0.3?
我正在使用 Jest 来测试我的 Node 应用程序。
我是否可以期望/断言一个值是一个日期对象?
expect(typeof result).toEqual(typeof Date())
是我的尝试,但自然返回[Object]。所以这也会通过 {}。
谢谢!
大家好我需要能够将折线中心放在地图的中心
只有代码的必要部分......
success: function(data) {//callback to be executed when the response has been received
data = JSON.parse(data);
for (var i=0;i<data.length;i++) {
flightPlanCoordinates[i] = new google.maps.LatLng(data[i].x,data[i].y);
}
map = new google.maps.Map(document.getElementById("map_find"),
mapOptions);
flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: "#8a2be2",
strokeOpacity: 1.0,
strokeWeight: 3
});
flightPath.setMap(map);
map.setZoom(5);
map.setCenter(flightPlanCoordinates);
}
Run Code Online (Sandbox Code Playgroud)
这不是我需要的.我的地图不是居中而不是缩放,如何实现这一目标?
我有 this dayjs 对象:
const today = dayjs.utc(date).startOf("day")
我试图用笑话来嘲笑它,但无济于事。这是我尝试过的方法:
jest.mock("dayjs", () => ({
extend: jest.fn(),
utc: jest.fn((...args) => {
const dayjs = jest.requireActual("dayjs");
dayjs.extend(jest.requireActual("dayjs/plugin/utc"));
return dayjs
.utc(args.filter((arg) => arg).length > 0 ? args : mockDate)
.startOf("day");
}),
startOf: jest.fn().mockReturnThis(),
}));
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个:
jest.mock("dayjs", () => ({
extend: jest.fn(),
utc: jest.fn((...args) => ({
startOf: jest.fn(() => {
const dayjs = jest.requireActual("dayjs");
dayjs.extend(jest.requireActual("dayjs/plugin/utc"));
return dayjs
.utc(args.filter((arg) => arg).length > 0 ? args : mockEventData)
.startOf("day");
}),
})),
}));
Run Code Online (Sandbox Code Playgroud)
两者都不工作。有人有建议吗?
我尝试用玩笑来模拟 npm 模块 jsonwebtoken 的验证功能。该函数返回一个已解码的令牌,但我想将此函数的自定义返回传递给我的单元测试。
我明确请求在继续请求之前检查访问令牌的有效性。但我想模拟令牌检查的时刻以直接返回用户值。并且轻松通过这一步。我把代码中关注的部分给你了。
但打字稿向我发送此错误: 类型 '{ 上不存在属性 'mockReturnValue' (令牌:字符串,secretOrPublicKey:秘密,选项?:VerifyOptions | 未定义):字符串 | 目的; (令牌:字符串,secretOrPublicKey:字符串 | 缓冲区 | { 密钥:字符串 | 缓冲区;密码:字符串; } | GetPublicKeyOrSecret,回调?:VerifyCallback | 未定义):void;(令牌:字符串,secretOrPublicKey:字符串| ... ...'。
所以模拟不起作用,我不明白。我遵循 Jest.io 上的模拟 axios 步骤,但它似乎不适用于 jsonwebtoken。
每个人都知道问题是什么或者如何在 jest 上模拟这个 jsonwebtoken 模块吗?
用户.test.ts
import jwt from 'jsonwebtoken'
jest.mock('jwt')
jwt.verify.mockReturnValue({
userId: String(member._id),
email: String(member.email),
permissionLevel: member.permissionLevel,
username: String(member.username),
})
describe('### /GET users', () => {
it('it should return 200 (Users List)', async (done) => {
const res = await request(app).set('Authorization', 'Bearer').get('/users') …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Jest 和 Knex 测试 GraphQL 服务器。我很难弄清楚如何在打字稿中使用 knexfile。但是现在除了测试之外,开发和生产环境一切正常。
这是我的当前knexfile.ts:
// knexfile.ts
const defaults = {
client: 'pg',
connection: {
host: DB_HOST,
user: DB_USER,
password: DB_PASSWORD,
database: DB_DATABASE
},
pool: {
min: 2,
max: 10
},
migrations: {
extension: 'ts',
directory: './migration',
tableName: 'knex_migrations'
},
seeds: {
extension: 'ts',
directory: './seed'
}
};
interface KnexConfig {
[key: string]: object;
}
const knexConfig: KnexConfig = {
local: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
}
},
development: {
...defaults,
debug: …Run Code Online (Sandbox Code Playgroud) 我需要追加 - 到目前的文本框值而不替换当前文本.我试过这个代码.
if(len.length==4){
$("-").appendTo("#date").val();
}
Run Code Online (Sandbox Code Playgroud)
但它失败了.
我试图测试我的 React 类,它有import dotnetify from "dotnetify";导入。这工作正常,但 Jest 说,dotnetify 是未定义的。如果我更改为const dotnetify = require("dotnetify");,Jest 通过测试,但这是愚蠢的解决方法。如何解释 Jest,那个 dotnetify 不是未定义的?
比你提前。
javascript ×5
jestjs ×5
node.js ×4
typescript ×2
center ×1
dayjs ×1
ecmascript-6 ×1
github ×1
google-maps ×1
jquery ×1
jwt ×1
knex.js ×1
mocking ×1
polyline ×1
reactjs ×1
testing ×1
unit-testing ×1
validation ×1
webpack ×1
webpack-2 ×1
webpack-3 ×1
yarnpkg ×1